Przejdź do głównej zawartości

std::count() algorithm

// (1)
template< class InputIt, class T >
constexpr typename iterator_traits<InputIt>::difference_type
count( InputIt first, InputIt last, const T& value );

// (2)
template< class ExecutionPolicy, class ForwardIt, class T >
typename iterator_traits<ForwardIt>::difference_type
count( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last, const T& value );

Returns the number of elements equal to value in the range [first1; last1).

  • (1) Counts the elements that are equal to value (using operator==).
  • (2) Same as (1), but executed according to policy.
    Overload Resolution

    These overloads participate in overload resolution only if std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>  (do C++20) std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>  (od C++20) is true.

Parameters

first
last

The range of elements to examine.

value

The value to search for.

policy

The execution policy to use. See execution policy for details.

Type requirements

InputItLegacyInputIterator
ForwardItLegacyForwardIterator

Return value

  • (1 - 2) The number of elements that are equal to value.

Complexity

Given N as std::distance(first, last):

  • (1 - 2) exactly N comparisons with value using operator==

Exceptions

The overloads with a template parameter named ExecutionPolicy report errors as follows:

  • If execution of a function invoked as part of the algorithm throws an exception and ExecutionPolicy is one of the standard policies, std::terminate is called. For any other ExecutionPolicy, the behavior is implementation-defined.
  • If the algorithm fails to allocate memory, std::bad_alloc is thrown.

Possible implementation

count (1)
template<class InputIt, class T>
typename iterator_traits<InputIt>::difference_type
count(InputIt first, InputIt last, const T& value)
{
typename iterator_traits<InputIt>::difference_type ret = 0;
for (; first != last; ++first)
if (*first == value)
++ret;
return ret;
}

Notes

If you want to obtain the number of elements in range [first; last) without any additional criteria, use std::distance

Examples

Main.cpp
#include <algorithm>
#include <array>
#include <iostream>
#include <iterator>

int main()
{
constexpr std::array v {1, 2, 3, 4, 4, 3, 7, 8, 9, 10};
std::cout << "v: ";
std::copy(v.cbegin(), v.cend(), std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';

// determine how many integers match a target value.
for (const int target: {3, 4, 5})
{
const int num_items = std::count(v.cbegin(), v.cend(), target);
std::cout << "number: " << target << ", count: " << num_items << '\n';
}

// use a lambda expression to count elements divisible by 4.
int count_div4 = std::count_if(v.begin(), v.end(), [](int i) { return i % 4 == 0; });
std::cout << "numbers divisible by four: " << count_div4 << '\n';

// A simplified version of `distance` with O(N) complexity:
auto distance = [](auto first, auto last)
{
return std::count_if(first, last, [](auto) { return true; });
};
static_assert(distance(v.begin(), v.end()) == 10);
}
Output
v: 1 2 3 4 4 3 7 8 9 10
number: 3, count: 2
number: 4, count: 2
number: 5, count: 0
numbers divisible by four: 3
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::count() algorithm

// (1)
template< class InputIt, class T >
constexpr typename iterator_traits<InputIt>::difference_type
count( InputIt first, InputIt last, const T& value );

// (2)
template< class ExecutionPolicy, class ForwardIt, class T >
typename iterator_traits<ForwardIt>::difference_type
count( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last, const T& value );

Returns the number of elements equal to value in the range [first1; last1).

  • (1) Counts the elements that are equal to value (using operator==).
  • (2) Same as (1), but executed according to policy.
    Overload Resolution

    These overloads participate in overload resolution only if std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>  (do C++20) std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>  (od C++20) is true.

Parameters

first
last

The range of elements to examine.

value

The value to search for.

policy

The execution policy to use. See execution policy for details.

Type requirements

InputItLegacyInputIterator
ForwardItLegacyForwardIterator

Return value

  • (1 - 2) The number of elements that are equal to value.

Complexity

Given N as std::distance(first, last):

  • (1 - 2) exactly N comparisons with value using operator==

Exceptions

The overloads with a template parameter named ExecutionPolicy report errors as follows:

  • If execution of a function invoked as part of the algorithm throws an exception and ExecutionPolicy is one of the standard policies, std::terminate is called. For any other ExecutionPolicy, the behavior is implementation-defined.
  • If the algorithm fails to allocate memory, std::bad_alloc is thrown.

Possible implementation

count (1)
template<class InputIt, class T>
typename iterator_traits<InputIt>::difference_type
count(InputIt first, InputIt last, const T& value)
{
typename iterator_traits<InputIt>::difference_type ret = 0;
for (; first != last; ++first)
if (*first == value)
++ret;
return ret;
}

Notes

If you want to obtain the number of elements in range [first; last) without any additional criteria, use std::distance

Examples

Main.cpp
#include <algorithm>
#include <array>
#include <iostream>
#include <iterator>

int main()
{
constexpr std::array v {1, 2, 3, 4, 4, 3, 7, 8, 9, 10};
std::cout << "v: ";
std::copy(v.cbegin(), v.cend(), std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';

// determine how many integers match a target value.
for (const int target: {3, 4, 5})
{
const int num_items = std::count(v.cbegin(), v.cend(), target);
std::cout << "number: " << target << ", count: " << num_items << '\n';
}

// use a lambda expression to count elements divisible by 4.
int count_div4 = std::count_if(v.begin(), v.end(), [](int i) { return i % 4 == 0; });
std::cout << "numbers divisible by four: " << count_div4 << '\n';

// A simplified version of `distance` with O(N) complexity:
auto distance = [](auto first, auto last)
{
return std::count_if(first, last, [](auto) { return true; });
};
static_assert(distance(v.begin(), v.end()) == 10);
}
Output
v: 1 2 3 4 4 3 7 8 9 10
number: 3, count: 2
number: 4, count: 2
number: 5, count: 0
numbers divisible by four: 3
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.