Getting information

Once you have values in a container, you can call functions to get information about those items. The count function is used to count the number items with a specified value in a range:

    vector<int> planck{ 6,6,2,6,0,7,0,0,4,0 };     auto number = count(planck.begin(), planck.end(), 6);

This code will return a value of 3 because there are three copies of 6 in the container. The return type of the function is the type specified in the difference_typetypedef of the container, and in this case it will be int. The count_if function works in a similar way, but you pass a predicate that takes a single parameter (the current item in the container) and returns a bool specifying if this is the value that is being counted.

The count ...

Get Beginning C++ Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.