C++ std::count_if() Function

Definition
In C++, std::count_if() counts the elements for which a predicate returns true.
Use it when you need more than equality, for example "how many scores are 10 or more?" or "how many words start with c?".
Syntax
Add #include <algorithm>, then call std::count_if(). Start with these two lines.
std::count_if(v.begin(), v.end(), pred);
std::count_if(v.begin(), v.end(), [](int n) { return n >= 10; });
| Call | What it does |
|---|
std::count_if(v.begin(), v.end(), pred); | Count the elements that pass the test. |
std::count_if(v.begin(), v.end(), [](int n) { return n >= 10; }); | Write the rule inline with a lambda. |
Library declarations
template<class InputIt, class UnaryPred>
typename iterator_traits<InputIt>::difference_type
count_if(InputIt first, InputIt last, UnaryPred p);
Parameters
| Parameter | Description |
|---|
| first, last | Required. The range to check. |
| p | Required. A predicate that returns true for elements you want to count. |
Return Value
| Return Value | Description |
|---|
| Number | How many elements made the predicate return true. |
C++ Examples
Example 1 — Count large values
Count values that are at least 10.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {3, 8, 12, 5, 20, 7};
int n = std::count_if(v.begin(), v.end(), [](int x) { return x >= 10; });
std::cout << "Values at least 10: " << n << '\n';
return 0;
}
How It Works
- The lambda is true when a number is 10 or more.
- Only 12 and 20 pass. The other four numbers fail.
count_if() returns the number of passes, 2.
Example 2 — Count even numbers
Count how many numbers are even.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 4, 5, 6, 7};
auto evens = std::count_if(v.begin(), v.end(), [](int x) { return x % 2 == 0; });
std::cout << "Even numbers: " << evens << " of " << v.size() << '\n';
return 0;
}
How It Works
x % 2 == 0 is the standard test for an even number.- 2, 4 and 6 pass it.
- We print the count next to
v.size() to show how many were tested.
Example 3 — Count words
Count words that start with the letter c.
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
int main() {
std::vector<std::string> words = {"cat", "dog", "cow", "bird", "cub"};
int n = std::count_if(words.begin(), words.end(),
[](const std::string& w) { return !w.empty() && w[0] == 'c'; });
std::cout << "Words starting with c: " << n << '\n';
return 0;
}
How It Works
- The lambda checks the first character of each word.
!w.empty() protects against reading the first letter of an empty string.- cat, cow and cub pass. dog and bird do not.
Frequently Asked Questions
In C++, std::count_if() counts how many elements satisfy a condition. Include <algorithm> to use it.
Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
It returns the number of elements for which the predicate returns true.
count() counts elements equal to a value. count_if() counts elements that make a predicate return true.
Yes. Use a lambda capture such as [limit](int x) { return x > limit; }.
Next: equal()
Checks whether two ranges hold the same elements in the same order.
equal() tutorial →About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
12 people found this page helpful