##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::basic_string::operator[]

From cppreference.com
< cpp‎ | string‎ | basic string

reference operator[]( size_type pos ); |  |
const_reference operator[]( size_type pos ) const; |  |

Returns a reference to the character at specified location `pos`. No bounds checking is performed.

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 pos == size(), a reference to the character with value CharT() (the null character) is returned. For the first (non-const) version, the behavior is undefined if this character is modified. | (since C++11)

### [edit] Parameters

pos | - | position of the character to return

### [edit] Return value

Reference to the requested character.

### [edit] Complexity

Constant.

### [edit] Example

Run this code

    #include <iostream>
    #include <string>

    int main()
    {
      {
        std::string const c("Exemplar");
        for (unsigned i = 7;i != 0; i/=2)
            std::cout << c[i];
      }
        std::cout << '\n';
      {
        std::string s("Exemplar ");
        s[s.size()-1] = 'y';
        std::cout << s;
      }
    }

Output:

    rmx
    Exemplary

### [edit] See also

at | access specified character with bounds checking (public member function) [edit]

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/string/basic_string/operator_at&oldid=70490"