Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Iterators Good reference site:

Similar presentations


Presentation on theme: "1 Iterators Good reference site:"— Presentation transcript:

1 1 Iterators Good reference site: http://www.sgi.com/tech/stl/table_of_contents.html

2 2 Iterators We have discussed the major containers provided by the STL. In addition to these, users can write their own containers. We often need certain algorithms for different containers. For example, we want to be able to find elements in a list, or in a tree. We want to be able to sort a list or sort a vector. We may want to add up all the elements of a list or all the elements of a vector.

3 3 Sequences and Iterators One approach to this problem would be to write separate sort or sum or search methods for these containers. A better approach would be to write only one version of sort or sum and somehow be able to apply it to any container. This means that we must find a way to manipulate a container without knowing exactly what kind it is and how it is implemented. We focus on the notion of a sequence of elements. The sequence has a beginning and an end. We use iterators to refer to elements and tools that allow us to access the next element of the sequence. An iterator is an abstraction of the notion of a pointer to an element of a sequence. Any object that behaves like an iterator is an iterator. Iterators are the glue that holds containers and algorithms together!

4 4 Sequences and Iterators Sequences arrays vectors singly- and doubly-linked lists trees input output There are several types of iterators. Different types support different operations. Random access Bidirectional Forward Input/Output more powerful

5 5 Iterators Category Operation ReadAccessWriteIterateCompare Input Output Forward Random-Access Bi-directional *i= =*i –>–> –>–> –>–> –>–>[ ] ++ – ++ – += – = + – == != ==!= ==!= ==>= <= > <

6 6 Iterators Container Type Supported Random accessBi-directional vector list deque queue stack None!          

7 7 Iterators & Algorithms Remember: Iterators provide a link between containers and algorithms Example: We wish to write a function sum() that adds up the members of a sequence 1 Idea 1: Write a separate sum() for each type of sequence (list, vector, etc.) Idea 2: Write sum() as a function template that can take a sequence as an argument Idea 3: Write sum() as a function template that can take range-specifying iterators as arguments. 1 The STL actually provides such an algorithm: accumulate

8 8 STL Algorithms find() Returns the first iterator i in the range [first, last) such that *i == value, or last if no such iterator exists. template InputIterator find(InputIterator first, InputIterator last, const EqualityComparable& value);

9 9 STL Algorithms count() Returns the number of iterators i in [first, last) such that *i == value template iterator_traits ::difference_type count(InputIterator first, InputIterator last, const EqualityComparable& value);

10 10 STL Algorithms remove() Returns an iterator new_last such that the range [first, new_last) contains no elements equal to value. template ForwardIterator remove(ForwardIterator first, ForwardIterator last, const T& value);

11 11 STL Algorithms reverse() Reverses the specified range. template void reverse(BidirectionalIterator first, BidirectionalIterator last);

12 12 STL Algorithms copy() Copies elements from the range [first, last) to the range [result, result + (last - first)) template OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result);

13 13 STL Algorithms Several STL algorithms require a functional argument. e.g. Consider our employee database. We may want to remove all employees who satisfy the requirement "the last evaluation report was very bad". A suitable function would be something like: remove_if (first, last, bad_eval()) where bad_eval() is a function 1 that examines an element's evaluation number and returns T if it is below some threshold. Or, we may want to sort all employees based on their evaluation number. We will then need to do something like sort(first, last, greaterThan()) where greaterThan() defines a comparison between employees, based on their evaluations. 1 Functions that return T or F are called predicates.

14 14 STL Algorithms Some functions that take predicates: find_if() remove_if() replace_if() min(), max()


Download ppt "1 Iterators Good reference site:"

Similar presentations


Ads by Google