std::basic_string::front(3)
π NAME π
std::basic_string::front β std::basic_string::front.
Opening vibe check: this is the std::basic_string::front(3) page from the C++ Standard Libary manual β¨ (yes, that spelling is in the header, iykyk). We are literally here to talk about the first character of a string, and bestie, it is giving main character energy π.
π Synopsis π
CharT& front(); (1) (constexpr since C++20)
const CharT& front() const; (2) (constexpr since C++20)
So front() returns a reference to the first character in the string. The behavior is undefined if empty() is true β that is the dramatic warning, bestie π. Do not test an empty string with this and then act surprised.
Two overloads, both marked (constexpr since C++20) in the synopsis π
.
π«Ά Parameters π«Ά
(none) β no parameters, no arguments, no little extras. Zero. Nada. π
β¨ Return value β¨
Reference to the first character, equivalent to operator[](0) π.
β‘ Complexity β‘
Constant. That is the whole complexity section, and it ate π₯.
π Notes π
In libstdc++, front() is not available in C++98 mode. That is the note, fr fr π.
π§ͺ Example π§ͺ
// Run this code
#include <iostream>
#include <string>
int main()
{
std::string s("Exemplary");
char& f1 = s.front();
f1 = 'e';
std::cout << s << '\n'; // "exemplary"
std::string const c("Exemplary");
char const& f2 = c.front();
std::cout << &f2 << '\n'; // "Exemplary"
}
Output:
exemplary
Exemplary
The first one gets its first character changed from E to e, and the const one stays iconic and unchanged. Periodt π.
π©Ή Defect reports π©Ή
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- DR
LWG 534β applied to C++98.- Behavior as published:
std::basic_stringdid not have the member functionfront(). - Correct behavior:
front()was added.
- Behavior as published:
π See also π
backβ accesses the last character (DR*) (public member function).frontβ accesses the first character (public member function ofstd::basic_string_view<CharT,Traits>).
Footer check, bestie: http://cppreference.com, 2024.06.10, and std::basic_string::front(3) β source/version plus date, all accounted for π«Άπ. Bye bestie, go access that first character responsibly π
β¨.
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms