std::basic_string<CharT,Traits,Allocator>::operator[]
reference operator[]( size_type pos ); | (1) | |
const_reference operator[]( size_type pos ) const; | (2) |
Returns a reference to the character at specified location pos. No bounds checking is performed. If pos > size(), the behavior is undefined.
1) If pos == size(), the behavior is undefined. 2) If pos == size(), a reference to the character with value CharT() (the null character) is returned. | (until C++11) |
| If For the first (non-const) version, the behavior is undefined if this character is modified to any value other than | (since C++11) |
Parameters
| pos | - | position of the character to return |
Return value
Reference to the requested character.
Complexity
Constant.
Example
#include <iostream>
#include <string>
int main()
{
std::string const 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';
std::cout << s << '\n';
}Output:
rmx Exemplar Exemplary
See also
| accesses the specified character with bounds checking (public member function) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/string/basic_string/operator_at