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, std::weakly_incrementable O,class T,class Proj=std::identity> | (since C++20) (until C++26) | |
template<std::input_iterator I,std::sentinel_for<I> S, std::weakly_incrementable O,class Proj=std::identity, | (since C++26) | |
(2) | ||
template<ranges::input_range R, std::weakly_incrementable O,class T,class Proj=std::identity> | (since C++20) (until C++26) | |
template<ranges::input_range R, std::weakly_incrementable O,class Proj=std::identity, | (since C++26) | |
template<std::input_iterator I,std::sentinel_for<I> S, std::weakly_incrementable O,class Proj=std::identity, | (3) | (since C++20) |
template<ranges::input_range R, std::weakly_incrementable O,class Proj=std::identity, | (4) | (since C++20) |
Helper types | ||
template<class I,class O> using remove_copy_result=ranges::in_out_result<I, O>; | (5) | (since C++20) |
template<class I,class O> using remove_copy_if_result=ranges::in_out_result<I, O>; | (6) | (since C++20) |
Copies elements from the source range[
first,
last)
, to the destination range beginning atresult, omitting the elements which (after being projected byproj) satisfy specific criteria. The behavior is undefined if the source and destination ranges overlap.
The function-like entities described on this page arealgorithm function objects (informally known asniebloids), that is:
Contents |
first, last | - | the iterator-sentinel pair defining the sourcerange of elements to process |
r | - | the source range of elements |
result | - | the beginning of the destination range |
value | - | the value of the elementsnot to copy |
comp | - | the binary predicate to compare the projected elements |
proj | - | the projection to apply to the elements |
{last, result+ N}, whereN is the number of elements copied.
Exactlyranges::distance(first, last) applications of the corresponding predicatecomp and any projectionproj.
The algorithm is stable, i.e. preserves the relative order of the copied elements.
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_algorithm_default_value_type | 202403 | (C++26) | List-initialization for algorithms(1,2) |
remove_copy (1,2) |
---|
struct remove_copy_fn{template<std::input_iterator I,std::sentinel_for<I> S,std::weakly_incrementable O,class Proj=std::identity,class T= std::projected_value_t<I, Proj>> requiresstd::indirectly_copyable<I, O>&&std::indirect_binary_predicate<ranges::equal_to, std::projected<I, Proj>,const T*>constexpr ranges::remove_copy_result<I, O> operator()(I first, S last, O result,const T& value, Proj proj={})const{for(;!(first== last);++first)if(value!=std::invoke(proj,*first)){*result=*first;++result;}return{std::move(first), std::move(result)};} template<ranges::input_range R,std::weakly_incrementable O,class Proj=std::identity,class T= std::projected_value_t<ranges::iterator_t<R>, Proj>> requiresstd::indirectly_copyable<ranges::iterator_t<R>, O>&&std::indirect_binary_predicate<ranges::equal_to, std::projected<ranges::iterator_t<R>, Proj>,const T*>constexpr ranges::remove_copy_result<ranges::borrowed_iterator_t<R>, O> operator()(R&& r, O result,const T& value, Proj proj={})const{return(*this)(ranges::begin(r),ranges::end(r), std::move(result), value, std::move(proj));}}; inlineconstexpr remove_copy_fn remove_copy{}; |
remove_copy_if (3,4) |
struct remove_copy_if_fn{template<std::input_iterator I,std::sentinel_for<I> S,std::weakly_incrementable O,class Proj=std::identity,std::indirect_unary_predicate<std::projected<I, Proj>> Pred> requiresstd::indirectly_copyable<I, O>constexpr ranges::remove_copy_if_result<I, O> operator()(I first, S last, O result, Pred pred, Proj proj={})const{for(; first!= last;++first)if(false==std::invoke(pred,std::invoke(proj,*first))){*result=*first;++result;}return{std::move(first), std::move(result)};} template<ranges::input_range R,std::weakly_incrementable O,class Proj=std::identity,std::indirect_unary_predicate< std::projected<ranges::iterator_t<R>, Proj>> Pred> requiresstd::indirectly_copyable<ranges::iterator_t<R>, O>constexpr ranges::remove_copy_if_result<ranges::borrowed_iterator_t<R>, O> operator()(R&& r, O result, Pred pred, Proj proj={})const{return(*this)(ranges::begin(r),ranges::end(r), std::move(result), std::move(pred), std::move(proj));}}; inlineconstexpr remove_copy_if_fn remove_copy_if{}; |
#include <algorithm>#include <array>#include <complex>#include <iomanip>#include <iostream>#include <iterator>#include <string_view>#include <vector> void println(constauto rem,constauto& v){std::cout<< rem<<' ';for(constauto& e: v)std::cout<< e<<' ';std::cout<<'\n';} int main(){// Filter out the hash symbol from the given string.conststd::string_view str{"#Small #Buffer #Optimization"};std::cout<<"before: "<<std::quoted(str)<<'\n'; std::cout<<"after:\""; std::ranges::remove_copy(str.begin(), str.end(),std::ostream_iterator<char>(std::cout),'#');std::cout<<"\"\n"; // Copy only the complex numbers with positive imaginary part.using Ci=std::complex<int>;constexprstd::array<Ci,5> source{ Ci{1,0}, Ci{0,1}, Ci{2,-1}, Ci{3,2}, Ci{4,-3}};std::vector<std::complex<int>> target; std::ranges::remove_copy_if( source,std::back_inserter(target),[](int imag){return imag<=0;},[](Ci z){return z.imag();}); println("source:", source); println("target:", target); std::vector<std::complex<float>> nums{{2,2},{1,3},{4,8},{1,3}};std::vector<std::complex<double>> outs;#ifdef __cpp_lib_algorithm_default_value_typestd::remove_copy(nums.cbegin(), nums.cend(),std::back_inserter(outs),{1,3});// T gets deduced to std::complex<float>#elsestd::remove_copy(nums.cbegin(), nums.cend(),std::back_inserter(outs),std::complex<float>{1,3});#endif println("nums: ", nums); println("outs: ", outs);}
Output:
before: "#Small #Buffer #Optimization"after: "Small Buffer Optimization"source: (1,0) (0,1) (2,-1) (3,2) (4,-3)target: (0,1) (3,2)nums: (2,2) (1,3) (4,8) (1,3)outs: (2,2) (4,8)
(C++20)(C++20) | removes elements satisfying specific criteria (algorithm function object)[edit] |
(C++20)(C++20) | copies a range of elements to a new location (algorithm function object)[edit] |
(C++20) | copies a number of elements to a new location (algorithm function object)[edit] |
(C++20) | copies a range of elements in backwards order (algorithm function object)[edit] |
(C++20)(C++20) | copies a range, replacing elements satisfying specific criteria with another value (algorithm function object)[edit] |
(C++20) | creates a copy of a range that is reversed (algorithm function object)[edit] |
(C++20) | copies and rotate a range of elements (algorithm function object)[edit] |
(C++20) | creates a copy of some range of elements that contains no consecutive duplicates (algorithm function object)[edit] |
copies a range of elements omitting those that satisfy specific criteria (function template)[edit] |