STL C Standard Library continued STL Iterators u

STL: C++ Standard Library (continued) .

STL Iterators u. Iterators are allow to traverse sequences u. Methods · operator* · operator-> · operator++, and operator— u. Different types of iterators - to support read, write and random access u. Containers define their own iterator types u. Changing the container can invalidate the iterator

Iterator Types u Output: write only and can write only once u Input: read many times each item u Forward: supports both read and write u Bi-directional: supports also decrement u Random: supports random access (just like C pointer)

Iterators & Containers Bidirectional iterators: u list, map, set Random access iterators: uvector, deque Input/output/forward iterators: uiostreams

Iterators & Containers Every STL container T provides: class T { … typedef … iterator // iterator type T iterator begin(); // first element of the container iterator end(); // element after last of the container };

Iterators & Containers Typical code will look like: Container C … Container: : iterator i for( i = C. begin(); i != C. end(); i++) // do something with *i

Iterators & Containers Iterators allow to access/modify elements Container C; Container: : iterator i, j; u C. insert(i, x) – insert x before i u C. insert(i, first, last) – insert elements in [first, last) before i u C. erase(i) – erase element i points to u C. erase(i, j) – erase elements in range [i, j)

Iterator validity u. When working with iterators, we have to remember that their validity can change u. Whats wrong with this code? Container C; C: : iterator i; for( i = C. begin(); i != C. end(); i++ ) if( f( *i ) ) // some test C. erase(i); // remove this element

Iterator validity Second try… Container C; C: : iterator i = C. begin(); while( i != C. end() ) { C: : iterator j = i++; if( f( *j ) ) // some test C. erase(j); // remove this element } Works for set, map, list, not vector or deque

Iterator Validity u. Third try… Container C; C: : iterator i; for(i = C. begin(); i != C. end(); i++ ) if( f( *i ) ) // some test i= C. erase(i); // erase returns the iterator for the next element in the container. Still does not work for vector.

Iterators and Map Suppose we work with map<string, int> dictionary; map<string, int>: : iterator i; … i = dictionary. begin(); What is the type of *i ?

Iterators and Map Every STL container type Container defines Container: : value_type Type of elements stored in container u. This is the type returned by an iterator Container: : value_type Container: : iterator operator*();

Iterators and Map u. Ok, so what type of elements does a map return (what is value_type of map) ? Recall keeps pairs · Key. Type key – “key” of entry · Data. Type value – “value” of entry u map<Key. Type, Data. Type>

Pairs template< class T 1, class T 2> struct pair { typedef T 1 first_type; typedef T 2 first_type; T 1 first; T 2 second; pair( const T 1& x, const T 2& y ) : first(x), second(y) {} };

Map value_type template< class Key, class T, class Cmp = less<Key> > class map { public: typedef pair<const Key, T> value_type; typedef Key key_type; typedef T data_type; typedef Cmp key_compare; };

Using map iterator map<string, int> dict; … map<string, int>: : iterator i; for( i = dict. begin(); i != dict. end(); i++ ) { cout << i->first << “ “ << i->second << “n”; } [See dictionary. cpp]

Iterators and Assoc. Containers Additional set of operations: u iterator C: : find(key_type const& key) Return iterator to first element with key. Return end() if not found u iterator C: : lower_bound(key_type const& key) Return iterator to first element greater or equal to key u iterator C: : upper_bound(key_type const& key) Return iterator to first element greater than key

Iterators & Streams Can access iostreams through iterators: istream_iterator<string> in(cin); istream_iterator<string> end. Of. Input; ostream_iterator<string> out(cout); while( in != end. Of. Input ) { string s = *in++; *out++ = s; *out++ = “ “; } see use. Stream. Iterators. cpp

Inserters istream_iterator<string> in(cin); istream_iterator<string> end. Of. Input; vector<string> vect; back_insert_iterator<vector<string> > back(vect); // copy input words into vector… while( in != end. Of. Input ) *back++ = *in++; // same as: vect. push_back(*in++)

Inserters are output iterators u back_insert_iterator<C>( C& c) Insert at back of c u front_insert_iterator<C>( C& c) Insert at front of c u insert_iterator<C, Iter>(C& c, Iter i) Insert at just before i Allow to write into containers in generic algorithms

Do-it-yourself iterators u. You can create iterators Check list: · Define the appropriate operators · Ensure copy constructor/operator · Define the right typedefs use inheritance from iterator<. . > class [See Token. Iterator. h]

Sequence Adapters u. Adapters of basic containers u. Very easy, but limited interface stack<T, Seq> uprovides push, pop, top, size and empty queue<T, Seq> ualso provides back priority_queue<T, Seq, Cmp> uprovides same interface as stack uuses a compare function object
![Container summary vector [] List ops const n+ list deque const Front ops Back Container summary vector [] List ops const n+ list deque const Front ops Back](http://slidetodoc.com/presentation_image_h/1b411497e914075910f4d1c3dd1d2b66/image-23.jpg)
Container summary vector [] List ops const n+ list deque const Front ops Back ops Iterators const+ Random const Bi-direct n const Random stack const queue const priority_queue log(n) map set log(n)+ Bi-direct

Algorithms Overview u. Sequence operations · Non modifying: for_each, find, count, search, mismatch, equal · Modifying: transform, copy, swap, replace, fill, generate, remove, unique, reverse, rotate, random_shuffle u. Sorted sequences operations · sort, lower_bound, upper_bound, equal_range, binary_search, merge, includes, set_union, intersection, set_difference, set_symmetric_difference

Algorithms u. Most STL algorithms works on sequences u. Sequences are passed as two iterators: · beginning element · element one after last p q sequence [p, q) u. Algorithms depend on iterator type not on container type

Copy template< class In, class Out> Out copy(In first, In last, Out res) { while (first != last) *res++ = *first++; return res; } See use. Copy. cpp

Non-modifying Sequence Algorithms u In find(In first, In last, const T& val) find the first occurence of val in the sequence u In find_if(In first, In last, Pred p) find the first element satisfying p u I 1 find_first_of(I 1 f 1, I 2 l 1, I 2 f 2, I 2 l 2) find the first match between two sequences. u I 1 search( I 1 f 1, I 1 l 1, I 2 f 1, I 2 l 2 ) search for the sequence f 2. . . l 2 inside f 1. . l 1
![Sorted Sequence Algorithms u sort(In first, In last[, class cmp]) find the first occurence Sorted Sequence Algorithms u sort(In first, In last[, class cmp]) find the first occurence](http://slidetodoc.com/presentation_image_h/1b411497e914075910f4d1c3dd1d2b66/image-28.jpg)
Sorted Sequence Algorithms u sort(In first, In last[, class cmp]) find the first occurence of val in the sequence In lower_bound(In first, In last, T const & val[, class cmp]) find the first element not less than val u bool binary_search(In first, In last, T const & val[, class cmp]) check if val appears in the sequence u Out merge( I 1 f 1, I 1 l 1, I 2 f 1, I 2 l 2, Out out ) u merge two sorted sequences and write the merged sequence onto the output iterator out

Ex 8: Huffman Code u. Very easy !! u. Compression of text files, implement using STL: u. Priority queue u. Map u. Vector/deque u. Function objects u. Copy (algorithm) u. The more STL you’ll use the more you’ll learn.
- Slides: 29