Containers
Many classes in the STL (Standard Template Library) like list, vector and string are containers that store and provide sequential or direct access to a number of entities. The string class is a container that holds chars. All container classes access the contained elements safely and efficiently by using iterators.
A container class includes members such as
- begin
- clear
- erase(iterator i)
- erase(iterator first,iterator last)
- find
Iterators
An iterator is like a smart pointer that is used to walk through the elements of a container without knowing what the container holds. There are five iterator types in the STL but with strings we're only interested in the Random Access Iterator type. For efficiency, with strings an iterator is implemented a char *.
Iterators work with containers. The principle is that a container produces two iterators that define a sequence or subrange of values in the container. Then an STL algorithm (a function implemented as a function template) can be processed for each value in the sequence.
All iterators provide begin() and end() where begin() points to the start i.e. the first char, but end() always points one element past the last char.
string s("Humpty Dumpty sat on a wall.");
string::iterator e = s.end()-1; // Points to period
cout << "Last Char is [" << (char *)e << "]" << endl;
On the next page Template Strings Continued

