In the previous posts of this series, we have had a glance at *non-modifying sequence algorithms*, i.e., the algorithms that are supposed not to modify the elements of a sequence (or at least, the structure of the sequence; see the observations about `for_each`

.

In this installment, as anticipated, we will start looking at the algorithms for searching.

`find`

The STL `find`

algorithms, specified in section [`alg.find`

] of the C++11 standard, are useful when searching elements in the sequence that satisfy some requirements. The basic form has the prototype that follows:

1 | template<class InputIterator, class T> |

This algorithm looks for the element whose value is, well, *value*. This implies that the class `T`

has some comparison operator that allows the algorithm to see whether the current element is the right one. For example, if we have a class Card, for which we define an `operator==()`

, then we can use the algorithm as follows:

1 | class Card { |

The output of this program (compiled with clang 3.2.7) is:

Card is in the deck Card Seven was not in the deck

Notice that the return type of the algorithm is an iterator in the sequence, so to get the element we could dereference the iterator *it*.

`find_if`

, `find_if_not`

One might want to find an element based on some predicate. This is what the other two forms of `find`

are for. These are their prototypes:

1 | template<class InputIterator, class Predicate> |

As seen for the algorithms presented in previous posts, also in this case we can use both a functor or a closure. Let’s see an example with closures:

1 | int main() { |

In this example, we look for even numbers in a vector. Since the algorithm is defined for each sequence, we can use the iterator returned by the first invocation of `find_if`

to start another sequence; only we have to be careful to increment it, otherwise, we are going to get the value 2 again. The result, as expected, is:

Found even number: 2 Found another even number: 4

### Conclusions

As you might have noticed so far, the usage of algorithms is fairly intuitive, and they can be very powerful to express ideas.

In the next installments, we’ll see some other interesting variants of the `find`

algorithm. Stay tuned!