1 / 14

Iterators

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.

tonya
Télécharger la présentation

Iterators

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Iterators Joe Meehean

  2. 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

  3. 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

  4. 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

  5. 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

  6. Using Iterators for(vector<string>::iterator iter = words.begin(); iter!= words.end(); iter++){ cout<< *iter << endl; } • For-loop • Assume a vector<string> words

  7. 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?

  8. 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; } } }

  9. 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 } }

  10. 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

  11. 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 } }

  12. 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

  13. Using Iterators in STL template <typenameForwardIterator> ForwardIteratormax_element( ForwardIterator first,ForwardIteratorlast ) { ForwardIteratorlargest = first; for(; first != last; first++){ if (*largest < *first) largest = first; } } return largest; }

  14. Questions?

More Related