150 likes | 284 Views
Iterators. Joe Meehean. Concept. Iterator is placeholder in a collection Iterate through complex data structures set: unordered collection of unique items tree: non-linear data structure without ordering operator[]( int pos) makes no sense. Common Operations.
E N D
Iterators Joe Meehean
Concept • Iterator is placeholder in a collection • Iterate through complex data structures • set: unordered collection of unique items • tree: non-linear data structure • without ordering operator[](int pos) makes no sense
Common Operations Get item at the current position Move to next or previous item Remove the current item Check to see if there are more items
Iterators in C++ STL • Each container in the STL defines • iterator begin(): returns iterator to first item • iterator end(): returns iterator to item after the last item • Iterator Interface • *iter: returns a referenceto the current item • iter++: moves the iterator to the next item • ++iter: moves the iterator to the next item • iter == aList.end(): no more items • iter != aList.end(): more items
Using Iterators vector<string>::iterator iter = words.begin(); while( iter != words.end() ){ cout<< *iter << endl; iter++; } • While loop • assume a vector<string> words • iterator always a nested type of container
Using Iterators for(vector<string>::iterator iter = words.begin(); iter!= words.end(); iter++){ cout<< *iter << endl; } • For-loop • Assume a vector<string> words
Using Iterators • Say we want to print even numbers from a vector • we should be able to guarantee we won’t change the items in the vector • how can we guarantee this when *iter gives us a reference to the data?
Using Iterators void printEven( vector<int> v ){ for( vector<int>::iterator iter = v.begin(); iter != v.end(); iter++){ if( *iter% 2 == 0 ){ cout << *iter << endl; } } }
Using Iterators void printEven( vector<int> v ){ for( vector<int>::iterator iter = v.begin(); iter != v.end(); iter++){ if( *iter% 2 == 0 ){ cout << *iter << endl; } *iter = 15; // LEGAL, BUT NOT WHAT WE WANT } }
Using Iterators • const_iterator • STL containers also have iterators that provide constant access • const_iterator begin() const; • const_iterator end() const; • *const_iterreturns a reference to a constant
Using Iterators void printEven( const vector<int> &v ){ vector<int>::const_iterator = iter; for( iter = v.begin(); iter!= v.end(); iter++){ if( *iter & 2 == 0 ){ cout<< *iter << endl; } *iter = 15; // COMPILER ERROR } }
Using Iterators in STL • Iterators are also an ADT • we don’t need to know what the underlying data structure is • we only need to know that the iterator will give us access to the data, in some order • STL makes use of this to provide generic (type-independent) algorithms • max_element • sort • next_permutation & more
Using Iterators in STL template <typenameForwardIterator> ForwardIteratormax_element( ForwardIterator first,ForwardIteratorlast ) { ForwardIteratorlargest = first; for(; first != last; first++){ if (*largest < *first) largest = first; } } return largest; }