C++ std::count() Function

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

Definition

In C++, std::count() walks through a range and counts the elements that are equal to a value. The result is a number.

It is perfect for questions like "how many times does 2 appear?" or "how many letter a are in this word?".

Syntax

Add #include <algorithm>, then call std::count(). Start with these two lines.

C++
std::count(v.begin(), v.end(), 2);
std::count(s.begin(), s.end(), 'a');
CallWhat it does
std::count(v.begin(), v.end(), 2);Count the value 2 in the whole vector.
std::count(s.begin(), s.end(), 'a');Count one character in a string.
Library declarations
C++
template<class InputIt, class T>
typename iterator_traits<InputIt>::difference_type
    count(InputIt first, InputIt last, const T& value);

Parameters

ParameterDescription
first, lastRequired. The range to count in.
valueRequired. The value to count. Elements are compared with ==.

Return Value

Return ValueDescription
NumberHow many elements are equal to value. It is zero when there are none.

C++ Examples

Example 1 — Count a number

Count how many times 2 appears.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {1, 2, 2, 3, 2};
    int n = std::count(v.begin(), v.end(), 2);

    std::cout << "2 appears " << n << " times\n";
    return 0;
}

How It Works

  1. count() checks all five elements with ==.
  2. The values at index 1, 2 and 4 are equal to 2.
  3. The total, 3, is stored in n.

Example 2 — Count a letter

Count one character in a word.

C++
#include <algorithm>
#include <iostream>
#include <string>

int main() {
    std::string word = "banana";
    int n = std::count(word.begin(), word.end(), 'a');

    std::cout << "Letter a appears " << n << " times\n";
    return 0;
}

How It Works

  1. A string is a range of char, so count() can scan it.
  2. The letters at index 1, 3 and 5 are a.
  3. Single quotes are used because we count a character, not a string.

Example 3 — Count part of a vector

Count in a smaller range and in the whole range.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {5, 5, 1, 5, 5, 5};

    int firstThree = std::count(v.begin(), v.begin() + 3, 5);
    int all = std::count(v.begin(), v.end(), 5);

    std::cout << "First three: " << firstThree << '\n';
    std::cout << "All values: " << all << '\n';
    return 0;
}

How It Works

  1. v.begin() + 3 ends the first range after three elements.
  2. In 5, 5, 1 there are two fives.
  3. Using v.end() counts the whole vector, which has five fives.

Frequently Asked Questions

In C++, std::count() counts how many elements equal a given value. 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 in the range that are equal to the given value.
It returns 0.
Use std::count_if() and pass a predicate such as a lambda.

Next: count_if()

Counts how many elements satisfy a condition.

count_if() 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.

24 people found this page helpful