next up previous index
Next: Find the first Up: Non-mutating sequence operations Previous: Apply a function

Find an element satisfying a condition in a sequence

Source code

Declaration

template <class InputIterator, class T>
InputIterator find(InputIterator first, InputIterator last, const T& value);

template <class InputIterator, class Predicate>
InputIterator find_if(InputIterator first, InputIterator last,
                      Predicate pred);

Description      The find  function returns an iterator i referring to the first element in the range [ first, last) \ equal  to value, or i = last if no element in the range is equal  to value.

The find_if  function returns an iterator i referring to the first element in the range [ first, last) that makes the predicate  function object pred return true (i.e., a nonzero value), or i = last if the predicate  function object pred is false for all elements in the range.

In the first function checks for element equality are made with operator!=, while in the second they are made with the function object pred.

Type requirements

Group Non-mutating sequence operations.

Time complexity Linear.

If i last, then the number of applications of operator!= or pred is the size of the range [ first, i] ; otherwise, the number of applications of operator!= or pred is the size of the range [ first, last) .

Space complexity Constant.

Mutative? No.

Details If i last, then the locations in the range [ first, i] are dereferenced; those in the range ( i, last]

are not. If i = last, then all of the locations in the range [ first, last) are dereferenced.

Glossary terms constant space, dereference type, dereferencing an iterator, function object, function object type, InputIterator, iterator, linear time, mutative, operator !=, Predicate, predicate, range, sequence.

Example Select here to view the source code for the example.

 

Implementation Select here to view source code (Copyright (C) 1994, Hewlett-Packard Company) for this algorithm.

Implementation notes A sequential forward search   from first to last is performed.



next up previous index
Next: Find the first Up: Non-mutating sequence operations Previous: Apply a function



Kenny Zalewski
Mon May 13 04:03:40 EDT 1996