Constrained algorithms and algorithms on ranges(C++20) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Constrained algorithms, e.g.ranges::copy,ranges::sort, ... | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Execution policies(C++17) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Numeric operations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Operations on uninitialized memory | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
std::ranges
Non-modifying sequence operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Modifying sequence operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Partitioning operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Sorting operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Binary search operations (on sorted ranges) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Set operations (on sorted ranges) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Heap operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Minimum/maximum operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Permutation operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Fold operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Operations on uninitialized storage | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Return types | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Defined in header <algorithm> | ||
Call signature | ||
(1) | ||
template<std::input_iterator I,std::sentinel_for<I> S, class T,class Proj=std::identity> | (since C++20) (until C++26) | |
template<std::input_iterator I,std::sentinel_for<I> S, class Proj=std::identity, | (since C++26) | |
(2) | ||
template<ranges::input_range R,class T,class Proj=std::identity> requiresstd::indirect_binary_predicate | (since C++20) (until C++26) | |
template<ranges::input_range R,class Proj=std::identity, class T= std::projected_value_t<ranges::iterator_t<R>, Proj>> | (since C++26) | |
template<std::input_iterator I,std::sentinel_for<I> S, class Proj=std::identity, | (3) | (since C++20) |
template<ranges::input_range R,class Proj=std::identity, std::indirect_unary_predicate< | (4) | (since C++20) |
Returns the number of elements in the range[
first,
last)
satisfying specific criteria.
The function-like entities described on this page arealgorithm function objects (informally known asniebloids), that is:
Contents |
first, last | - | the iterator-sentinel pair defining therange 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 |
Number of elements satisfying the condition.
Exactlylast- first comparisons and projection.
For the number of elements in the range without any additional criteria, seestd::ranges::distance.
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_algorithm_default_value_type | 202403 | (C++26) | List-initialization for algorithms(1,2) |
count (1) |
---|
struct count_fn{template<std::input_iterator I,std::sentinel_for<I> S,class Proj=std::identity,class T= std::projected_value_t<I, Proj>> requiresstd::indirect_binary_predicate<ranges::equal_to, std::projected<I, Proj>,const T*>constexprstd::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 Proj=std::identityclass T= std::projected_value_t<ranges::iterator_t<R>, Proj>> requiresstd::indirect_binary_predicate<ranges::equal_to, std::projected<ranges::iterator_t<R>, Proj>,const T*>constexprranges::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));}}; inlineconstexpr count_fn count; |
count_if (3) |
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>constexprstd::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>constexprranges::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));}}; inlineconstexpr count_if_fn count_if; |
#include <algorithm>#include <cassert>#include <complex>#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'; std::vector<std::complex<double>> nums{{4,2},{1,3},{4,2}};#ifdef __cpp_lib_algorithm_default_value_typeauto c= ranges::count(nums,{4,2});#elseauto c= ranges::count(nums,std::complex<double>{4,2});#endifassert(c==2);}
Output:
number: 3 count: 2number: 5 count: 0number divisible by three: 3number divisible by eleven: 0
(C++20) | returns the distance between an iterator and a sentinel, or between the beginning and end of a range (algorithm function object)[edit] |
(C++20) | creates a subrange from an iterator and a count (customization point object)[edit] |
aview that consists of the elements of arange that satisfies a predicate(class template)(range adaptor object)[edit] | |
returns the number of elements satisfying specific criteria (function template)[edit] |