# count_if() in C++ STL

count_if() function returns the number of elements in a range that satisfy the condition.

Examples:

```Input: 0 1 2 3 4 5 6 7 8 9
Output: Total no of even numbers is: 5

Input: 2 3 4 5 6 7 8 9 10 11 12 13
Output: Total no of even numbers is: 6
```

Syntax:

```count_if(lower_bound, upper_bound, function)
```

The count_if function takes three parameters, the first two of which are the first and the last position of the sequence of the elements (where the last position is not included in the range) while the third parameter is the function that takes the element of given sequence one by one as a parameter and returns a boolean value on the basis of condition
specified in that function.
Then, count_if() returns the number of elements in the given sequence for which the comparator function
(third parameter) returns true.

 `// C++ program to show the working ` `// of count_if() ` `#include ` `using` `namespace` `std; ` ` `  `// Function to check the  ` `// number is even or odd ` `bool` `isEven(``int` `i) ` `{ ` `    ``if` `(i % 2 == 0) ` `        ``return` `true``; ` `    ``else` `        ``return` `false``; ` `} ` ` `  `// Drivers code ` `int` `main() ` `{ ` `    ``vector<``int``> v; ` `    ``for` `(``int` `i = 0; i < 10; i++) { ` `        ``v.push_back(i); ` `    ``} ` `    ``int` `noEven = count_if(v.begin(), v.end(), ` `                                     ``isEven); ` ` `  `    ``cout << ``"Total no of even numbers is: "`  `         ``<< noEven; ` ` `  `    ``return` `0; ` `} `

Output:

```Total no of even numbers is: 5
```

Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

4

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.