Scroll to navigation

std::ranges::count,std::ranges::count_if(3) C++ Standard Libary std::ranges::count,std::ranges::count_if(3)

NAME

std::ranges::count,std::ranges::count_if - std::ranges::count,std::ranges::count_if

Synopsis


Defined in header <algorithm>
Call signature
template< std::input_iterator I, std::sentinel_for<I> S,


class T, class Proj = std::identity >
requires std::indirect_binary_predicate<ranges::equal_to, (since
std::projected<I, Proj>, (1) C++20)
const T*>
constexpr std::iter_difference_t<I>


count( I first, S last, const T& value, Proj proj = {} );
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>, (2) (since
const T*> C++20)
constexpr ranges::range_difference_t<R>


count( R&& r, const T& value, Proj proj = {} );
template< std::input_iterator I, std::sentinel_for<I> S,


class Proj = std::identity, (since
std::indirect_unary_predicate<std::projected<I, Proj>> Pred > (3) C++20)
constexpr std::iter_difference_t<I>


count_if( I first, S last, Pred pred, Proj proj = {} );
template< ranges::input_range R, class Proj = std::identity,


std::indirect_unary_predicate<std::projected<ranges::iterator_t<R>, (since
Proj>> Pred > (4) C++20)
constexpr ranges::range_difference_t<R>


count_if( R&& r, Pred pred, Proj proj = {} );


Returns the number of elements in the range [first, last) satisfying specific
criteria.


1) counts the elements that are equal to value.
3) counts elements for which predicate p returns true.
2,4) Same as (1,3), 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, that is:


* Explicit template argument lists may not be specified when calling any of them.
* None of them is visible to argument-dependent lookup.
* When one of them is found by normal unqualified lookup for the name to the left
of the function-call operator, it inhibits argument-dependent lookup.


In practice, they may be implemented as function objects, or with special compiler
extensions.

Parameters


first, last - the range of elements to examine
r - the range of the elements to examine
value - the value to search for
pred - predicate to apply to the projected elements
proj - projection to apply to the elements

Return value


Number of elements satisfying the condition.

Complexity


Exactly last - first comparisons and projection.

Notes


For the number of elements in the range without any additional criteria, see
std::ranges::distance.

Possible implementation

First version

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;

Second version

struct count_if_fn {
template< std::input_iterator I, std::sentinel_for<I> S,
class Proj = std::identity,
std::indirect_unary_predicate<std::projected<I, Proj>> Pred >
constexpr std::iter_difference_t<I>
operator()( I first, S last, Pred pred, Proj proj = {} ) const
{
std::iter_difference_t<I> counter = 0;
for (; first != last; ++first) {
if (std::invoke(pred, std::invoke(proj, *first)))
{
++counter;
}
}


return counter;
}


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

inline constexpr count_if_fn count_if;

Example

// Run this code


#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

See also


ranges::distance returns the distance between an iterator and a sentinel, or
(C++20) between the beginning and end of a range
(niebloid)
views::counted creates a subrange from an iterator and a count
(C++20) (customization point object)
ranges::filter_view a view that consists of the elements of a range that satisfies a
views::filter predicate
(C++20) (class template) (range adaptor object)
count returns the number of elements satisfying specific criteria
count_if (function template)

2022.07.31 http://cppreference.com