November 2001
Beginner
1128 pages
29h 12m
English
There are four methods for accessing individual characters, two using the [] operator, and two using the at() method:
reference operator[](size_type pos); const_reference operator[](size_type pos) const; reference at(size_type n); const_reference at(size_type n) const;
The first operator[]() method allows you to access an individual element of a string using array notation; it can be used to retrieve or alter the value. The second operator[]() method can be used with const objects, and it can be used only to retrieve the value:
string word("tack");
cout << word[0]; // display the t
word[3] = 't'; // overwrite the k with a t
const ward("garlic");
cout << ward[2]; // display the r
The at() methods provide similar access, except ...
Read now
Unlock full access