1
The Standard Template Library
An introduction
2
Standard Template Library (STL)
• Objective: Reuse common code • Common constructs: Generic containers and algorithms • STL
Part of the C++ Standard Library Developed by Alexander Stepanov and Meng Lee at HP Powerful, template-based components:
Containers: template data structures Iterators: like pointers, access elements of containers Algorithms: data manipulation, searching, sorting, etc.
3
Common STL Containers*
Standard Library container class
Sequence containers
Description
vector deque list
Associative containers
fast insertions and deletions at back direct access to any element fast insertions and deletions at front or back direct access to any element doubly linked list, fast insertion and deletion anywhere
set multiset map multimap
Other container
fast lookup, no duplicates
fast lookup, duplicates allowed
one-to-one mapping, no duplicates, fast key-based lookup
one-to-many mapping, duplicates allowed, rapid key-based lookup
stack queue
last-in, first-out (LIFO) first-in, first-out (FIFO)
4
Iterators
• Iterators are objects that point to elements in a container • Iterators have overloaded operators:
– ++ (and --) that cause it to point to the next (previous) element in the container – operator * (dereferencing operator) that returns the element pointed to by the iterator – operator== and operator !=
• Most STL containers have their corresponding iterator class • containers have functions begin() that returns an iterator to the first element, and end() that returns an iterator to one after the last element
5
Types of Iterators
• Bidirectional
– can move forward and backwards
• Random access
– Like bidirectional, but can also jump to any element
• const iterators
– Cannot modify the object pointed by
• Reverse iterators
– Advance in opposite direction (back to front of the container)
• istream_iterator, ostream_iterator
– For Input and output sequences
6
Iterator Types Supported
• Sequence containers
– vector: random access – deque: random access – list: bidirectional
• Associative containers (all bidirectional)
– – – – set multiset map multimap
• Container adapters (no iterators supported)
– stack – queue
7
Iterator Operations
• All
– – – – ++p, p++ *p p = p1 p == p1, p != p1
• Bidirectional
– --p, p--
• Random access
– – – – – p + i, p += i p - i, p -= i p[i] p < p1, p <= p1 p > p1, p >= p1
8
istream_iterator
– std::istream_iterator inp(cin); • Read input from cin – type a = *inp; • Returns the next value from cin – ++inp; • Extracts a new value from cin – Default ctor constructs an end-of-stream iterator
9
ostream_iterator
– std::ostream_iterator outp(cout); • To output type values to cout – *outp = var; • Write var to cout – ++outp; • Advance iterator
istream_iterator and ostream_iterator Example
#include #include using std::cout; using std::cin; using std::endl; using std::ostream_iterator; using std::istream_iterator; int main() { const int maxlen=3; int i=0; vector vi(maxlen); ostream_iterator outp(cout);
10
cout<<"Enter "< inp(cin), eos; do{ vi.at(i)=*inp; }while(++i
#include using std::vector; typedef vector Vector;
#include using std::deque; typedef deque Deque;
#include using std::list; typedef list List; #include #include using std::cout; using std::cin; using std::endl; using std::ostream_iterator;
14
Algorithm Example
// function object class class Print { public: template void operator()(const T& printable) const { cout << printable << endl; } }; int main() { const int maxlen = 3; lmnt temp;
Vector container1; Deque container2; List container3(2*maxlen);
// Initialize Vector for(int i=0; i < maxlen ; ++i) { cin>>temp; container1.push_back(temp); }
15
Algorithm Example
// sort Vector Vector::iterator vstart=container1.begin(); Vector::iterator vend=container1.end(); sort(vstart, vend) ;
// Initialize Deque for(int i = 0; i < maxlen; i++) { cin>>temp; container2.push_front(temp); }
// sort Deque Deque::iterator dstart=container2.begin(); Deque::iterator dend=container2.end(); sort(dstart, dend) ;
// merge the elements of Vector and Deque // and place the results in List merge(vstart,vend,dstart,dend,container3.begin()) ;
16
Algorithm Example
cout << "\nSorted elements:" << endl ; for_each(container3.begin(), container3.end(), Print()); reverse(container3.begin(), container3.end()); cout << "\nAfter reversing:" << endl ; for_each(container3.begin(),container3.end(),Print()); cout<<"Enter element to search: "<>temp; List::iterator it=find(container3.begin(),container3.end(),temp); ostream_iterator os(cout); copy(it,container3.end(),os); // output elements to cout cout< > baseTypeVector;
instead of
vector baseTypeVector;