Think of an iterator as a smart index. You can also change the value in an iterator by adding or subtracting ints.
string s="A jolly nice string";In an STL container, the first element is accessed by the begin() method which returns an iterator. There's also one for end(). As the iterator is Random Access, it's possible to process the sequence in reverse and for that there a rbegin() and rend() as well. Using the string s in example 5, these two for loops print out the string forwards and backwards using an iterator and a reverse iterator.
string::iterator pos=s.begin() ;
pos += 2; // pos now points to j in jolly
// Forwards
for (string::iterator p = s.begin(); p<s.end(); p++)
cout << *p;
cout << endl;
// BackwardsThis outputs
for ( string::reverse_iterator rp=s.rbegin() ; rp < s.rend(); rp++ )
cout << *rp;
cout << endl;
New Name sat on a wall.Note Reverse iterators run from rbegin() to rend() even though they are iterating backwards. Remember, processing goes from begin to end, no matter which direction.
.llaw a no tas emaN weN
In the string class, there are template overloads of these functions : assign(), erase(), append(), replace(), and insert(). This example below, taken from example 7 shows erase() being used to remove the first part of a string.
string s("Humpty Dumpty sat on a wall.") ;
s2=s;
string::iterator parse = s2.begin()+ s2.find("Dumpty") ;
s2.erase(parse,parse+7) ;
cout << s2 << endl;
On the next page A small example of Text Processing.

