In C++, std::string::at() gives you a reference to one character by its index. Counting starts at 0.
at() is the safe indexed access. If the index is out of range, it throwsstd::out_of_range. The unchecked s[i] operator does not throw. Include <string>.
This is a member of std::string. A string grows as you append, stores characters contiguously, and can reallocate—after growth, old iterators and references may be invalid. Prefer at() when you want bounds checks; remember find returns npos when missing.
Syntax
Pass a zero-based index that must be less than size().
C++
char ch = s.at(0);
s.at(1) = 'X';
Call
What it does
s.at(i)
Returns a reference to the character at index i. Throws if i >= s.size().
cs.at(i)
On a const string, returns a const reference (read-only).
Prefer at() when the index might be wrong and you want a clear exception. Use s[i] in hot loops when you already know the index is valid.