I have forgotten

• http://facebook.com/
• https://www.google.com/accounts/o8/id
• https://me.yahoo.com

# count_if

Returns the number of elements that match a criterion View version details

### Key Facts

Gyroscopic Couple: The rate of change of angular momentum ( ) = (In the limit).
• = Moment of Inertia.
• = Angular velocity
• = Angular velocity of precession.

Blaise Pascal (1623-1662) was a French mathematician, physicist, inventor, writer and Catholic philosopher.

Leonhard Euler (1707-1783) was a pioneering Swiss mathematician and physicist.

## Definition

The count_if() algorithm is defined in the standard header <algorithm> and in the nonstandard backward-compatibility header <algo.h>.

## Interface

#include <algorithm>
template < class InputIterator, class Predicate >
typename iterator_traits<InputIterator>::difference_type count_if(
InputIterator first,
InputIterator last,
Predicate pred
);

Parameters:

Parameter Description
first An input iterator addressing the position of the first element in the range to be searched
last An input iterator addressing the position one past the final element in the range to be searched
pred User-defined predicate function object that defines the condition to be satisfied if an element is to be counted. A predicate takes single argument and returns true or false

## Description

The count_if algorithm counts the element in a range using a predicate instead of operator==.

## Return Value

The return value is the number of elements that satisfy the condition specified by the predicate.

## Complexity

The complexity is linear, there are last - first applications of pred.

### References

Example:
##### Example - count_if algorithm
Problem
Here is a simple program which illustrates how to use count_if() algorithm.
Workings
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;

bool greater10(int value)
{
return value >10;
}

int main()
{
vector<int> v1;
vector<int>::iterator Iter;

v1.push_back(10);
v1.push_back(20);
v1.push_back(10);
v1.push_back(40);
v1.push_back(10);

cout <<"v1 = ( ";
for (Iter = v1.begin(); Iter != v1.end(); Iter++)
cout <<*Iter<<" ";
cout <<")"<< endl;

vector<int>::iterator::difference_type result1;
result1 = count_if(v1.begin(), v1.end(), greater10);
cout <<"The number of elements in v1 greater than 10 is: "
<<result1<<"."<<endl;

return 0;
}
Solution
Output:

v1 = ( 10 20 10 40 10 )
The number of elements in v1 greater than 10 is: 2.
References