An iterator is a generalization of a pointer. Iterators are used to move through the elements in some range of a container. The operations ++
, −−
, and dereferencing *
are usually defined for an iterator.
Container classes with iterators have member functions end()
and begin()
that return iterator values such that you can process all the data in the container as follows:
for (p = c.begin(); p != c.end(); p++)
process *p //*p is the current data item.
The main kinds of iterators are
Forward iterators: ++
works on the iterator.
Bidirectional iterators: both ++
and −−
work on the iterator.
Random access iterators: ++
, −−
, and random access all work with the iterator.
With a constant iterator p
, the dereferencing operator *p
produces a read-only version of the element. With a mutable iterator p
, *p
can be assigned a value.
A bidirectional container has reverse iterators that allow your code to cycle through the elements in the container in reverse order.
The main container template classes in the STL are list
, which has mutable bidirectional iterators, and the template classes vector
and deque
, both of which have mutable random access iterators.
stack
and queue
are container adaptor classes, which means they are built on top of other container classes. A stack
is a last-in/first-out container. A queue
is a first-in/first-out container.
The set
, map
, multiset
, and multimap
container template classes store their elements in sorted order for efficiency of search algorithms. A set
is a simple collection of elements. A map
allows storing and retrieving by key values. The multiset
class allows repetitions of entries. The multimap
class allows a single key to be associated with multiple data items.
The STL includes template functions to implement generic algorithms with guarantees on their maximum running time.
Features added in C++11 include the std::array
class, regular expressions, threading, and smart pointers.