std::ranges::reverse_copy, std::ranges::reverse_copy_result, std::ranges::reverse_copy_truncated_result - cppreference.com

Defined in header

<algorithm>

Call signature

template<std::bidirectional_iteratorI,std::sentinel_for<I>S,std::weakly_incrementableO>requiresstd::indirectly_copyable<I,O>constexprranges::reverse_copy_result<I,O>reverse_copy(Ifirst,Slast,Od_first); (1) (since C++20)template<ranges::bidirectional_rangeR,std::weakly_incrementableO>requiresstd::indirectly_copyable<ranges::iterator_t<R>,O>constexprranges::reverse_copy_result<ranges::borrowed_iterator_t<R>,O>reverse_copy(R&&r,Od_first); (2) (since C++20)template</*execution-policy*/Ep,std::random_access_iteratorI,std::sized_sentinel_for<I>S,std::random_access_iteratorO,std::sized_sentinel_for<O>OutS>requiresstd::indirectly_copyable<I,O>ranges::reverse_copy_truncated_result<I,O>reverse_copy(Ep&&policy,Ifirst,Slast,Od_first,OutSd_last); (3) (since C++26)template</*execution-policy*/Ep,/*sized-random-access-range*/R,/*sized-random-access-range*/OutR>requiresstd::indirectly_copyable<ranges::iterator_t<R>,ranges::iterator_t<OutR>>ranges::reverse_copy_truncated_result<ranges::borrowed_iterator_t<R>,ranges::borrowed_iterator_t<OutR>>reverse_copy(Ep&&policy,R&&r,OutR&&d_r); (4) (since C++26)Helper types

template<classI,classO>usingreverse_copy_result=ranges::in_out_result<I,O>; (5) (since C++20)template<classI,classO>usingreverse_copy_truncated_result=ranges::in_in_out_result<I,I,O>; (6) (since C++26)For the definition of /*execution-policy*/, see

this page

; for the definition of /*sized-random-access-range*/, see

this page

.

Copies elements from the source range [first, last) or r to the destination range in reverse order.

1,2) The destination range begins at d_first.

If the source and destination ranges overlap, the behavior is undefined.

3,4) Same as (1,2), but executed according to policy, and the destination range is [d_first, d_end) or d_r. If the destination range is exhausted before reaching the beginning of the source range, the remaining elements in the source range will not be copied.

If the source range and the following range overlap, the behavior is undefined:

3)[d_first, ranges::next(d_first,ranges::distance(first,last),d_last))

4)[d_r.begin(), ranges::next(d_r.begin(),ranges::distance(r),d_r.end()))

The function-like entities described on this page are

algorithm function objects

(informally known as niebloids), that is:

Explicit template argument lists cannot be specified when calling any of them.

None of them are visible to

argument-dependent lookup

.

When any of them are found by

normal unqualified lookup

as the name to the left of the function-call operator,

argument-dependent lookup

is inhibited.

Parameters

first, last - the iterator-sentinel pair defining the source

range

r - the source range d_first - the beginning of the destination range d_end - the sentinel of the destination range d_r - the destination range policy - the

execution policy

to use Return value

1,2) A ranges::reverse_copy_result object where:

The data member

in

holds the past-the-end iterator of the source range.

The data member

out

holds the past-the-end iterator of the destination range.

3,4) A ranges::reverse_copy_truncated_result object where:

The data member

in1

holds the past-the-end iterator of the source range.

The data member

in2

holds an iterator to the last copied element in the source range, or the past-the-end iterator of the source range if no element is copied.

The data member

out

holds an iterator past the last assigned element in the destination range, or an iterator to the beginning of the destination range if no element is assigned.

Complexity

Given

N1 as ranges::distance(first,last) or ranges::distance(r),

N2 as ranges::distance(d_first,d_last) or ranges::distance(d_r):

1,2) Exactly N1 assignments.

3,4) Exactly min(N1,N2) assignments.

Exceptions

3,4) During the execution process:

If the temporary memory resources required for parallelization are not available,

std::bad_alloc

is thrown.

If an uncaught exception is thrown while accessing objects via an algorithm argument, the behavior is determined by the execution policy (for

standard policies

,

std::terminate

is invoked).

Notes

Implementations (e.g.

MSVC STL

) may enable vectorization when the both iterator types model

contiguous_iterator

and have the same value type, and the value type is

TriviallyCopyable

.

Possible implementation

See also the implementations in

MSVC STL

and

libstdc++

.

structreverse_copy_fn{template<std::bidirectional_iteratorI,std::sentinel_for<I>S,std::weakly_incrementableO>requiresstd::indirectly_copyable<I,O>constexprranges::reverse_copy_result<I,O>operator()(Ifirst,Slast,Oresult)const{autoret=ranges::next(first,last);for(;last!=first;*result=*--last,++result);return{std::move(ret),std::move(result)};}template<ranges::bidirectional_rangeR,std::weakly_incrementableO>requiresstd::indirectly_copyable<ranges::iterator_t<R>,O>constexprranges::reverse_copy_result<ranges::borrowed_iterator_t<R>,O>operator()(R&&r,Oresult)const{return(*this)(ranges::begin(r),ranges::next(ranges::begin(r),ranges::end(r)),std::move(result));}};inlineconstexprreverse_copy_fnreverse_copy{};Example

Run this code

#include<algorithm>#include<iostream>#include<string>intmain(){std::stringx{"12345"},y(x.size(),' ');std::cout<<x<<" → ";std::ranges::reverse_copy(x.begin(),x.end(),y.begin());std::cout<<y<<" → ";std::ranges::reverse_copy(y,x.begin());std::cout<<x<<'\n';}Output:

12345 → 54321 → 12345 See also