std::basic_...::operator[](3)
π std::basic_...::operator[](3) π
okay so bestie, this is the std::basic_...::operator[](3) page from the C++ Standard Libary manual, section 3, and it's literally all about std::basic_string::operator[]. no cap, we're getting into character-reference territory π
β¨ NAME β¨
std::basic_string::operator[] β yep, that's the whole name: std::basic_string::operator[]. iconic behavior, honestly π
πΈ Synopsis πΈ
CharT& operator[]( size_type pos ); (1) (constexpr since C++20)
const CharT& operator[]( size_type pos ) const; (2) (constexpr since C++20)
So here's the tea: this returns a reference to the character at the specified location pos if pos < size(), or a reference to CharT() if pos == size(). and bestie, hear me: no bounds checking is performed. none. zero. it's giving "you're on your own" π
If pos > size(), the behavior is undefined. like, not cute, not safe, just undefined fr fr.
For overload (1), if pos == size(), the behavior is undefined if the object referred by the returned reference is modified to any value other than CharT() (since C++11). so yeah, that's a very specific vibe check and you better respect it π
π¦ Parameters π¦
posβ position of the character to return.
π Return value π
*(begin() + pos) if pos < size(), or a reference to CharT() if pos == size(). that's it, that's the whole return-value moment β¨
β Complexity β
Constant. constant time, no cap, we love a predictable icon π
π Example π
the example has a // Run this code invitation, and here's the code exactly:
#include <iostream>
#include <string>
int main()
{
const std::string e("Exemplar");
for (unsigned i = e.length() - 1; i != 0; i /= 2)
std::cout << e[i];
std::cout << '\n';
const char* c = &e[0];
std::cout << c << '\n'; // print as a C string
// Change the last character of s into a 'y'
std::string s("Exemplar ");
s[s.size() - 1] = 'y'; // equivalent to s.back() = 'y';
std::cout << s << '\n';
}
π Output π
rmx
Exemplar
Exemplary
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. history really said "let's fix that" and honestly? slay π«Ά
- LWG 259 β applied to
C++98. As published: non-const overload could return const lvaluedata()[pos], which was ill-formed. Correct behavior: changed to return*(begin() + pos). - LWG 2475 β applied to
C++11. As published: ifpos == size(), the behavior of modifying the object referred by the returned reference was undefined. Correct behavior: well-defined if modified toCharT().
π₯ See also π₯
atβ accesses the specified character with bounds checking (public member function)frontβ accesses the first character (DR*) (public member function)backβ accesses the last character (DR*) (public member function)operator[]β accesses the specified character (public member function ofstd::basic_string_view<CharT,Traits>)
okay bestie, that's the whole page π source/version is http://cppreference.com, dated 2024.06.10, and the page title is std::basic_...::operator[](3). go forth and index responsibly, and remember: no bounds checking means the bounds are not checking you back ππ
π¦ 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