Section 21.13 Search Algorithms
Another category of algorithms are algorithms that find elements in a collection.
Subsection 21.13.1 Find and FindIf
Find and find if do what you might expect: they search for elements in a collection. The βifβ version accepts a function to use to decide if an element matches the search criteria while the βplainβ find accepts a value to search for.
To return their result, they use an iterator. It will either point at the found element or at the end of the range if no matching element is found.
It find( It first, It last, const T& value );
It find_if( It first, It last, UnaryPredicate p );
find does a forward search. It starts from the beginning and works towards the end, stopping at the first match. If we want to do a backwards search that begins at the end and works towards the beginning, we can use find_last (or find_last_if).
Subsection 21.13.2 Count
Perhaps we want to know how many matches there are for a certain value or that satisfy a certain condition.We donβt want to move them or remove them, we just want to know how many there are. In that case,
count or count_if have us covered.
The count algorithms are called just like find, but they return a count of the matching elements instead of an iterator to the first match
You have attempted of activities on this page.
