CharT& operator[]( size_type pos );
(1) (constexpr since C++20)const CharT& operator[]( size_type pos ) const;
(2) (constexpr since C++20)Returns a reference to the character at specified location pos if pos < size(), or if pos == size():
1)The behavior is undefined.
(until C++11)Returns a reference to CharT(), if the object referred by the returned reference is modified to any value other than CharT(), the behavior is undefined.
(since C++11)2) Returns a reference to CharT().
If pos > size() is true, the behavior is undefined.
(until C++26)If pos > size() is true:
1) *(begin() + pos) if pos < size(), or a reference to CharT() if pos == size()(since C++11).
2) *(begin() + pos) if pos < size(), or a reference to CharT() if pos == size().
[edit] ComplexityConstant.
[edit] Example#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:
[edit] Defect reportsThe following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior LWG 259 C++98 overload (1) could return const lvaluestd::basic_string_view<CharT,Traits>
) [edit]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4