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.
Common STL Containers* 3 Standard Library container class Sequence containers vector deque list Associative containers set multiset map multimap Other container stack queue Description 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 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 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 Bidirectional Types of Iterators 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
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 6
Iterator Operations 7 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<type> 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<type> outp(cout); To output type values to cout *outp = var; Write var to cout ++outp; Advance iterator
istream_iterator and ostream_iterator Example #include <iostream> #include <iterator> using std::cout; using std::cin; using std::endl; using std::ostream_iterator; using std::istream_iterator; 10 int main() { const int maxlen=3; int i=0; vector<int> vi(maxlen); ostream_iterator<int> outp(cout); cout<<"enter "<<maxlen<<" integers:"<<endl; istream_iterator<int> inp(cin), eos; do{ vi.at(i)=*inp; }while(++i<maxlen && inp++!= eos); } for(i=0; i< maxlen; ++i, ++outp) { *outp=vi.at(i); cout<<" "; }
Algorithms 11 STL has algorithms used generically across containers Operate on elements indirectly via iterators Often operate on sequences of elements defined by pairs of iterators Common algorithms: find for_each copy remove replace sort merge
Function Objects 12 In STL function objects replace the role of function pointers A function object is an object of a class that overloads the function cal operator (operator() ) The call to operator() resembles a function call class FunctionClass { public: void operator()(int n){...} }; //... FunctionClass func; func(5);
Algorithm Example 13 #include "student.h" typedef Student lmnt; #include <algorithm> #include <vector> using std::vector; typedef vector<lmnt> Vector; #include <deque> using std::deque; typedef deque<lmnt> Deque; #include <list> using std::list; typedef list<lmnt> List; #include <iostream> #include <iterator> using std::cout; using std::cin; using std::endl; using std::ostream_iterator;
Algorithm Example 14 // function object class class Print { public: template<class T> 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); }
Algorithm Example 15 // 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()) ;
Algorithm Example 16 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: "<<endl; cin>>temp; List::iterator it=find(container3.begin(),container3.end(),temp); } ostream_iterator<lmnt> os(cout); copy(it,container3.end(),os); // output elements to cout cout<<endl;
STL and Polymorphism 17 Objects inserted into an STL container are copied according to their type. Thus, any class for which an STL container is constructed should have a copy constructor. Another consequence is that an STL container cannot be polymorphic. If we want to construct a polymorphic STL container based on some base type, it has to contain smart pointers to this type (see tutorial 12 for more details.) vector<smart_ptr<student> > basetypevector; instead of vector<student > basetypevector;