Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Iterators & Algorithms Iterators provide a link between containers and algorithms Example: We wish to write a function sum() that adds up the members.

Similar presentations


Presentation on theme: "1 Iterators & Algorithms Iterators provide a link between containers and algorithms Example: We wish to write a function sum() that adds up the members."— Presentation transcript:

1 1 Iterators & Algorithms 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 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

2 2 STL Algorithms We will discuss some of the STL algorithms. A good reference for them is the SGI site: http://www.sgi.com/tech/stl/table_of_contents.html

3 3 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);

4 4 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);

5 5 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);

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

7 7 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);

8 8 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. See examples on tlab... 1 Functions that return T or F are called predicates.

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


Download ppt "1 Iterators & Algorithms Iterators provide a link between containers and algorithms Example: We wish to write a function sum() that adds up the members."

Similar presentations


Ads by Google