count_if

September 1, 2019 ยท View on GitHub

Description : Returns the number of elements in the range [first, last) satisfying specific criteria(counts the elements that are equal to value).

Example:

    std::vector<int> v{ 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 };
 
    // use a lambda expression to count elements divisible by 3.
    int num_items3 = std::count_if(v.begin(), v.end(), [](int i){return i % 3 == 0;});
    std::cout << "number divisible by three: " << num_items3 << '\n';

Run Code