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 functions count the number of occurrences of a specific value. If you want to aggregate all the values, then you can use the accumulate function in <numeric>. This will iterate over the range, access each item and keep a running sum of all the items.

The sum will be carried out using the + operator of the type, but there is also a version that takes a binary function (two parameters of the container type and returns the same type) that specifies what happens when you add two such types together.

The all_of, any_of, and none_of functions are passed a predicate with a single argument of the same type of the container; there are also given iterators indicating a range over which they iterate, testing each item with the predicate. The all_of function will return true only if the predicate is true for all items, the any_of function returns true if predicate is true for at least one of the items, and the none_of function will return true only if the predicate is false for all items.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset