Skip to main content

std::ranges::count() algorithm

// (1)
constexpr std::iter_difference_t<I>
count( I first, S last, const T& value, Proj proj = {} );

// (2)
constexpr ranges::range_difference_t<R>
count( R&& r, const T& value, Proj proj = {} );

The type of arguments are generic and have the following constraints:

  • I - std::input_iterator
  • S - std::sentinel_for<I>
  • R - std::ranges::input_range
  • T - (none)
  • P - (none)

The Proj template argument has a default type of std::identity for all overloads.

Additionally, each overload has the following constraints:

  • (1) - indirect_binary_predicate<ranges::equal_to, projected<I, Proj>, const T*>
  • (2) - indirect_binary_predicate<ranges::equal_to, projected<ranges::iterator_t<R>, Proj>, const T*>

(The std:: namespace was ommitted here for readability)

Returns the number of elements equal to value in the given range.

  • (1) Counts the elements that are equal to value (using operator==).
  • (2) Same as (1), but uses r as the source range, as if using ranges::begin(r) as first and ranges::end(r) as last.

The function-like entities described on this page are niebloids.

Parameters

first
last

The range of elements to examine.

r

The range of elements to examine.

value

The value to search for.

proj

Projection to apply to the elements.

Return value

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

Complexity

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

  • (1 - 2) exactly N comparisons and projections.

Exceptions

(none)

Possible implementation

ranges::count
struct count_fn
{
template<std::input_iterator I, std::sentinel_for<I> S,
class T, class Proj = std::identity>
requires std::indirect_binary_predicate<ranges::equal_to, std::projected<I, Proj>,
const T*>
constexpr std::iter_difference_t<I>
operator()(I first, S last, const T& value, Proj proj = {}) const
{
std::iter_difference_t<I> counter = 0;
for (; first != last; ++first)
if (std::invoke(proj, *first) == value)
++counter;
return counter;
}

template<ranges::input_range R, class T, class Proj = std::identity>
requires std::indirect_binary_predicate<ranges::equal_to,
std::projected<ranges::iterator_t<R>, Proj>,
const T*>
constexpr ranges::range_difference_t<R>
operator()(R&& r, const T& value, Proj proj = {}) const
{
return (*this)(ranges::begin(r), ranges::end(r), value, std::ref(proj));
}
};

inline constexpr count_fn count;

Notes

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

Examples

Main.cpp
#include <algorithm>
#include <iostream>
#include <vector>

int main()
{
std::vector<int> v {1, 2, 3, 4, 4, 3, 7, 8, 9, 10};

namespace ranges = std::ranges;

// determine how many integers in a std::vector match a target value.
int target1 = 3;
int target2 = 5;
int num_items1 = ranges::count(v.begin(), v.end(), target1);
int num_items2 = ranges::count(v, target2);
std::cout << "number: " << target1 << " count: " << num_items1 << '\n';
std::cout << "number: " << target2 << " count: " << num_items2 << '\n';

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

// use a lambda expression to count elements divisible by 11.
int num_items11 = ranges::count_if(v, [](int i) {return i % 11 == 0;});
std::cout << "number divisible by eleven: " << num_items11 << '\n';
}
Output
number: 3 count: 2
number: 5 count: 0
number divisible by three: 3
number divisible by eleven: 0
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::ranges::count() algorithm

// (1)
constexpr std::iter_difference_t<I>
count( I first, S last, const T& value, Proj proj = {} );

// (2)
constexpr ranges::range_difference_t<R>
count( R&& r, const T& value, Proj proj = {} );

The type of arguments are generic and have the following constraints:

  • I - std::input_iterator
  • S - std::sentinel_for<I>
  • R - std::ranges::input_range
  • T - (none)
  • P - (none)

The Proj template argument has a default type of std::identity for all overloads.

Additionally, each overload has the following constraints:

  • (1) - indirect_binary_predicate<ranges::equal_to, projected<I, Proj>, const T*>
  • (2) - indirect_binary_predicate<ranges::equal_to, projected<ranges::iterator_t<R>, Proj>, const T*>

(The std:: namespace was ommitted here for readability)

Returns the number of elements equal to value in the given range.

  • (1) Counts the elements that are equal to value (using operator==).
  • (2) Same as (1), but uses r as the source range, as if using ranges::begin(r) as first and ranges::end(r) as last.

The function-like entities described on this page are niebloids.

Parameters

first
last

The range of elements to examine.

r

The range of elements to examine.

value

The value to search for.

proj

Projection to apply to the elements.

Return value

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

Complexity

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

  • (1 - 2) exactly N comparisons and projections.

Exceptions

(none)

Possible implementation

ranges::count
struct count_fn
{
template<std::input_iterator I, std::sentinel_for<I> S,
class T, class Proj = std::identity>
requires std::indirect_binary_predicate<ranges::equal_to, std::projected<I, Proj>,
const T*>
constexpr std::iter_difference_t<I>
operator()(I first, S last, const T& value, Proj proj = {}) const
{
std::iter_difference_t<I> counter = 0;
for (; first != last; ++first)
if (std::invoke(proj, *first) == value)
++counter;
return counter;
}

template<ranges::input_range R, class T, class Proj = std::identity>
requires std::indirect_binary_predicate<ranges::equal_to,
std::projected<ranges::iterator_t<R>, Proj>,
const T*>
constexpr ranges::range_difference_t<R>
operator()(R&& r, const T& value, Proj proj = {}) const
{
return (*this)(ranges::begin(r), ranges::end(r), value, std::ref(proj));
}
};

inline constexpr count_fn count;

Notes

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

Examples

Main.cpp
#include <algorithm>
#include <iostream>
#include <vector>

int main()
{
std::vector<int> v {1, 2, 3, 4, 4, 3, 7, 8, 9, 10};

namespace ranges = std::ranges;

// determine how many integers in a std::vector match a target value.
int target1 = 3;
int target2 = 5;
int num_items1 = ranges::count(v.begin(), v.end(), target1);
int num_items2 = ranges::count(v, target2);
std::cout << "number: " << target1 << " count: " << num_items1 << '\n';
std::cout << "number: " << target2 << " count: " << num_items2 << '\n';

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

// use a lambda expression to count elements divisible by 11.
int num_items11 = ranges::count_if(v, [](int i) {return i % 11 == 0;});
std::cout << "number divisible by eleven: " << num_items11 << '\n';
}
Output
number: 3 count: 2
number: 5 count: 0
number divisible by three: 3
number divisible by eleven: 0
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.