Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Section 3.5, class notes Iterators Generic algorithms.

Similar presentations


Presentation on theme: "1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Section 3.5, class notes Iterators Generic algorithms."— Presentation transcript:

1 1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Section 3.5, class notes Iterators Generic algorithms

2 2 Iterators: Motivation Need a way to navigate through the items in a container An example: navigating over vector v. for (int i = 0; i != v.size(); i++ ) cout << v[i] << endl; However, doubly-linked list would need a different form We want a general approach to navigate elements for different implementations of an ADT

3 3 Iterators A generalized type that helps in navigating a container –A way to initialize at the front and back of a list –A way to move to the next or previous position –A way to detect the end of an iteration –A way to retrieve the current value Implemented as nested types of containers in STL Examples: –Iterator type for vector defined as vector ::iterator itr; –Iterator type for list defined as list ::iterator itr;

4 4 Getting an Iterator Two methods in all STL containers –iterator begin ( ) Returns an iterator to the first item in the container –iterator end ( ) Returns an iterator representing end marker in the container (that is, the position after the last item) Example for (int i = 0; i != v.size(); i++ ) cout << v[i] << endl; can be written using iterators as for(vector ::iterator itr=v.begin(); itr!=v.end(); itr++) cout << itr++ << endl;

5 5 Iterator Methods Iterators have methods Many methods use operator overloading –itr++ and ++itr advance the iterator to next location Consider int A[8], int *i=A+2 –cout << *++i prints value of A[3] –cout << *i++ prints A[2], but i points to A[3] after this operation is complete –*itr returns reference to object stored at iterator itr’s location – itr1 == itr2 is true if itr1 and itr2 refer to the same location, else false – itr1 != itr2 is true if itr1 and itr2 refer to different locations, else it is false

6 6 Implementing Iterators Example: Lec11/DLL.h –An iterator class is associated with each container type –Its main data is a pointer –The container typedefs the iterator class and also the type of value that it stores The latter is helpful in implementing generic algorithms –Note the difference between pre-increment and post-increment operators DLLIterator &operator++()DLLIterator operator++(int) NodePtr = old value NodePtr = new value Returned NodePtr = old value NodePtr = new value Returned NodePtr = old Temporary

7 7 Generic Algorithms Give you flexibility in the container type and data type used –Enabled by common naming schemes for operations and typedefs of iterator and value_type –Example: Lec11/FindMax.cpp


Download ppt "1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Section 3.5, class notes Iterators Generic algorithms."

Similar presentations


Ads by Google