... endl; 21  }

string1 = Testing iterators
(Using iterator iterator1) string1 is: Testing iterators

Lines 8–9 declare string string1 and string::const_iterator iterator1. Recall that a const_iterator cannot be used to modify the data that you’re iterating through— in this case the string. Iterator iterator1 is initialized to the beginning of string1 with the string class member function begin. Two versions of begin exist—one that returns an iterator for iterating through a non-const string and a const version that returns a const_iterator for iterating through a const string. Line 11 outputs string1.

Lines 15–18 use iterator iterator1 to “walk through” string1. Class string member function end returns an iterator (or a const_iterator) for the ...

Get C++ How to Program, 10/e now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.