3.5. Using the Generic Algorithms

To use the generic algorithms, we must include the associated algorithm header file:

#include <algorithm> 

Let’s exercise the generic algorithms with our numeric sequence vector. is_elem() must return true if a value is an element in the sequence; otherwise, it returns false. Four possible generic search algorithms are as follows:

  1. find() searches an unordered collection marked by the iterator pair first,last for some value. If the value is found, find() returns an iterator addressing the value; otherwise, it returns an iterator addressing last.

  2. binary_search() searches a sorted collection. It returns true if the value is found; otherwise, it returns false. binary_search() is more efficient than find().

Get Essential C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.