Home » C++ algorithm count_if() function

C++ algorithm count_if() function

by Online Tutorials Library

C++ Algorithm Function count_if ()

C++ Algorithm count_if() function has a ‘pred’ value and returns the count of the elements in the range [first,last) for which the value of pred is true.

Syntax

Parameter

first: It is an input iterator to the first element in the range.

last: It is an input iterator to the last element in the range.

val: It is the element whose occurrence is being searched in the range.

Return value

The function returns the number of element in the range [first,last) for which the value of pred is true.

Example 1

Output:

newvector contains 5 odd values.  

Example 2

Output:

Count of even number is: 10  

Complexity

The complexity of the function is linear up to a distance between first and last element.

Data races

Some or all of the elements of the range are accessed

Exceptions

The function throws an exception if any of the argument throws one.

You may also like