19.11. Iterators

Class string provides iterators (introduced in Chapter 15) for forward and backward traversal of strings. Iterators provide access to individual characters with a syntax that’s similar to pointer operations. Iterators are not range checked. Figure 19.10 demonstrates iterators.

 1   // Fig. 19.10: Fig19_10.cpp 2   // Using an iterator to output a string. 3   #include <iostream> 4   #include <string> 5   using namespace std; 6  7   int main() 8   { 9      string string1( "Testing iterators" );10      string::const_iterator iterator1 = string1.begin();11 12      cout << "string1 = " << string113         << "\n(Using iterator iterator1) string1 is: ";14 15      // iterate through string                                  ...

Get C++11 for Programmers, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.