count_if
Syntax:
#include <algorithm> size_t count_if( iterator start, iterator end, UnaryPred p ); The count_if() function returns the number of elements between
For example, the following code uses count_if() with a predicate that returns true for the integer 3 to count the number of items in an array that are equal to 3: int nums[] = { 0, 1, 2, 3, 4, 5, 9, 3, 13 }; int start = 0; int end = 9; int target_value = 3; int num_items = count_if( nums+start, nums+end, bind2nd(equal_to<int>(), target_value) ); cout << "nums[] contains " << num_items << " items matching " << target_value << endl; When run, the above code displays the following output: nums[] contains 2 items matching 3 |