C++ std::count_if() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<algorithm> · C++98

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.

C++
std::count_if(v.begin(), v.end(), pred);
std::count_if(v.begin(), v.end(), [](int n) { return n >= 10; });
CallWhat 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
C++
template<class InputIt, class UnaryPred>
typename iterator_traits<InputIt>::difference_type
    count_if(InputIt first, InputIt last, UnaryPred p);

Parameters

ParameterDescription
first, lastRequired. The range to check.
pRequired. A predicate that returns true for elements you want to count.

Return Value

Return ValueDescription
NumberHow many elements made the predicate return true.

C++ Examples

Example 1 — Count large values

Count values that are at least 10.

C++
#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

  1. The lambda is true when a number is 10 or more.
  2. Only 12 and 20 pass. The other four numbers fail.
  3. count_if() returns the number of passes, 2.

Example 2 — Count even numbers

Count how many numbers are even.

C++
#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

  1. x % 2 == 0 is the standard test for an even number.
  2. 2, 4 and 6 pass it.
  3. 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.

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

  1. The lambda checks the first character of each word.
  2. !w.empty() protects against reading the first letter of an empty string.
  3. 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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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