Element Access
Accessing a character in a string is very easy and similar to element access in arrays.
We'll cover the following
Access to the elements of a string str
is very convenient because the strings support random access iterators. We can access with str.front()
the first character and with str.back()
the last character of the string. With str[n]
and str.at(n)
we get the n-th element by index.
The following table provides an overview.
Access the elements of the string #
Methods | Example |
---|---|
str.front() |
Returns the first character of str . |
str.back() |
Returns the last character of str . |
str[n] |
Returns the n-th character of str . The string boundaries will not be checked. |
str.at(n) |
Returns the n-th character of str . The string boundaries will be checked. If the boundaries are violated a std::out_of_range exception is thrown. |