Finding elements in a range
One of the most common operations we do in any application is searching through data. Therefore, it is not surprising that the standard library provides many generic algorithms for searching through standard containers or anything that can represent a range and is defined by a start and a past-the-end iterator. In this recipe, we will see what these standard algorithms are and how they can be used.
Getting ready
For all the examples in this recipe, we will use std::vector
, but all algorithms work with ranges defined by a begin and past-the-end, either input or forward iterators, depending on the algorithm (for more information about the various types of iterators, see the recipe, Writing your own random access iterator). All these algorithms are available in the std
namespace in the <algorithm>
header.
How to do it...
The following is a list of algorithms that can be used for finding elements in a range:
- Use
std::find()
to find a value in a range; this algorithm...