The STL algorithm library, supported by the algorithm and numeric header files, provides a large number of nonmember, iterator-based template functions. As discussed in Chapter 16, the template parameter names are chosen to indicate what concept particular parameters should model. For example, ForwardIterator is used to indicate that a parameter should, at the minimum, model the requirements of a forward iterator, and Predicate is used to indicate a parameter that should be a function object with one argument and a bool return value. The C++ Standard divides the algorithms into four groups: nonmodifying sequence operations, mutating sequence operations, sorting and related operators, and numeric operations. (C++11 moves numeric operations from the STL to the numeric library, but that doesn’t affect how they are used.) The term
Nonmodifying Sequence Operations
Table G.13 summarizes the nonmodifying sequence operations. Arguments are not shown, and overloaded functions are listed just once. A fuller description, including the prototypes, follows the table. Thus, you can scan the table to get an idea of what a function does and then look up the details if you find the function appealing.
Table G.13. Nonmodifying Sequence Operations
Now let’s take a more detailed look at these nonmodifying sequence operations. For each function, the discussion shows the prototype(s), followed by a brief explanation. Pairs of iterators indicate ranges, with the chosen template parameter name indicating the type of iterator. As usual a range in the form [first, last) goes from first up to, but not including, last. Some functions take two ranges, which need not be in the same kind of container. For example, you can use equal() to compare a list to a vector. Functions passed as arguments are function objects, which can be pointers (of which function names are an example) or objects for which the () operation is defined. As in Chapter 16, a predicate is a Boolean function with one argument, and a binary predicate is a Boolean function with two arguments. (The functions need not be type bool, as long as they return 0 for false and a nonzero value for true.)
all_of() (C++11)
template
bool all_of(InputIterator first, InputIterator last,
Predicate pred);
The all_of() function returns true if pred(*i) is true for every iterator in the range [first,last) or if the range is empty. Otherwise the function returns false.
any_of() (C++11)
template
bool any_of(InputIterator first, InputIterator last,
Predicate pred);
The any_of() function returns false if pred(*i) is false for every iterator in the range [first,last) or if the range is empty. Otherwise the function returns true.
none_of() (C++11)
template
bool none_of(InputIterator first, InputIterator last,
Predicate pred);
The none_of() function returns true if pred(*i) is false for every iterator in the range [first,last) or if the range is empty. Otherwise the function returns false.
for_each()
template
Function for_each(InputIterator first, InputIterator last,
Function f);
The for_each() function applies function object f to each element in the range [first, last). It also returns f.
find()
template
InputIterator find(InputIterator first, InputIterator last,
const T& value);
The find() function returns an iterator to the first element in the range [first, last) that has the value value; it returns last if the item is not found.
find_if()
template
InputIterator find_if(InputIterator first, InputIterator last,
Predicate pred);
The find_if() function returns an iterator it to the first element in the range [first, last) for which the function object call pred(*i) is true; it returns last if the item is not found.
find_if_not()
template
InputIterator find_if_not(InputIterator first, InputIterator last,
Predicate pred);
The find_if_not() function returns an iterator, it, to the first element in the range [first, last) for which the function object call pred(*i) is false; it returns last if the item is not found.
find_end()
template
ForwardIterator1 find_end(
ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2);