Standard Template Library
Iterators and Containers
Why STL
tasks like implementing a (templated) list of elements arise often in programming practice a library of templated classes that provide a set of templates for common tasks and ways of manipulating them is a part of C++ it is called Standard Template Library (STL) two main constructs containers – (templated) classes that simplify data manipulation iterators – ways of accessing elements containers
Containers
sequence containers – element access by position in container vector – indexed access to any element, quick insertions of elements in back only deque – indexed access, quick insertion/deletion from front/back list – sequential access only, quick insertion/deletion anywhere associative containers – element access by value (will not study) example: set - rapid lookup of elements, no duplicates allowed container declaration syntax: container_class container_name; where container_class - vector, deque, list, etc. type_parameter- type/class of elements container_name – identifier example vector items; // declares vector with no elements
Common Container Functions
c.empty() const; // true if there are no entries in c c.size() const; // number of entries in container c c.erase(); // removes one or more elements of the container c.clear(); // removes all elements from the container c = v; // replace contents of c with contents of v c.swap(v) // swaps contents of c and v
Iterators
iterator is a generalization of pointer not a pointer but usually implemented using pointers pointer operations may be overloaded for behaviour appropriate for the container treating iterators as pointers typically is OK each container defines an appropriate iterator type declaring iterator container_class ::iterator_type iterator_name; where container_class – vector, deque, list, etc. type_parameter – type/class of elements iterator_type – iterator, const_iterator (more later) iterator_name – identifier example
vector ::iterator p;
Iterators Operations
operations ++ (pre- and postfix) and -- to advance to the next (previous) data item = = and != operators to test whether two iterators point to the same data item dereferencing (*) provides data item access member functions
c.begin() returns an iterator pointing to the first element of container c c.end() returns an iterator pointing past the last element of container c
– analogous to the null pointer. Unlike the null pointer, you can apply – to the iterator returned by c.end() to get an iterator pointing to last element in the container
Example Usage
vector l; // declare vector … for ( // declare and initialize iterator vector::iterator p = l.begin(); p < l.end(); // check if end is not reached p++ // move iterator to next element ){ cout << *p; // manipulate element in loop body }
Iterator Types
Ordinary iterators are mutable – value can be assigned to the element iterator points to vector ::iterator p; *p = something; // legal constant iterators disallow assignment, makes syntactic error checking easier vector ::const_iterator p; *p = something; // illegal reverse iterator allows traversing container in from end to beginning vector ::reverse_iterator p; operators ++ and -- have opposite meaning – ++ advances iterator one element back functions rbegin() and rend() return iterators to end and begin of the container there is reverse constant iterator vector ::const_reverse_iterator p;
Operation Support
Operation Function vector List deque
Insert at front
Insert at back Delete at front Delete at back Insert in middle Delete in middle Sort
push_front(e)
push_back(e) pop_front( ) pop_back( ) insert(e) erase(iter ) sort( )
X X (X) (X) X
X
X X X X X -
X
X X X (X) (X) X
(X) this operation is significantly slower.