std::ranges...copy_result(3)
π std::ranges::reverse_copy,std::ranges::reverse_copy_result π
okay so bestie, welcome to the std::ranges...copy_result(3) page from the C++ Standard Library manual β this one is literally about copying a range in reverse and it's serving constexpr main character energy. π¦β¨ vibe check: passed.
πΈ NAME πΈ
std::ranges::reverse_copy, std::ranges::reverse_copy_result β the reverse-copy duo. That's the name, periodt. π
β¨ Synopsis β¨
Defined in header <algorithm>.
π Call signature
(1) (since C++20)
template< std::bidirectional_iterator I, std::sentinel_for<I> S,
std::weakly_incrementable O >
requires std::indirectly_copyable<I, O>
constexpr reverse_copy_result<I, O>
reverse_copy( I first, S last, O result );
(2) (since C++20)
template< ranges::bidirectional_range R, std::weakly_incrementable O >
requires std::indirectly_copyable<ranges::iterator_t<R>, O>
constexpr reverse_copy_result<ranges::borrowed_iterator_t<R>, O>
reverse_copy( R&& r, O result );
π§ Helper types
(3) (since C++20)
template< class I, class O >
using reverse_copy_result = ranges::in_out_result<I, O>;
okay so overload (1) copies the elements from the source range [first, last) to the destination range [result, result + N), where N is ranges::distance(first, last), in such a way that the elements in the new range are in reverse order. It behaves as if by executing the assignment *(result + N - 1 - i) = *(first + i) once for each integer i in [0, N). Undefined behavior alert π¨: the behavior is undefined if the source and destination ranges overlap.
fr fr, do NOT let those ranges overlap. It's giving chaos and the standard says undefined behavior. π
(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, 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.
In practice, they may be implemented as function objects, or with special compiler extensions.
π Parameters π
first,lastβ the range of elements to copy.rβ the range of elements to copy.resultβ the beginning of the destination range.
π Return value π
{last, result + N}. That's the whole return value, bestie. β¨
π¦ Complexity π¦
Exactly N assignments. No extra sprinkle, no cap. π
π 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. Lowkey iconic optimization behavior. π₯
π§ Possible implementation π§
See also the implementations in MSVC STL and libstdc++.
struct reverse_copy_fn
{
template<std::bidirectional_iterator I, std::sentinel_for<I> S,
std::weakly_incrementable O>
requires std::indirectly_copyable<I, O>
constexpr ranges::reverse_copy_result<I, O>
operator()(I first, S last, O result) const
{
auto ret = ranges::next(first, last);
for (; last != first; *result = *--last, ++result);
return {std::move(ret), std::move(result)};
}
template<ranges::bidirectional_range R, std::weakly_incrementable O>
requires std::indirectly_copyable<ranges::iterator_t<R>, O>
constexpr
ranges::reverse_copy_result<ranges::borrowed_iterator_t<R>, O>
operator()(R&& r, O result) const
{
return (*this)(ranges::begin(r), ranges::end(r),
std::move(result));
}
};
inline constexpr reverse_copy_fn reverse_copy {};
π Example π
// Run this code
#include <algorithm>
#include <iostream>
#include <string>
int main()
{
std::string x {"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 π
ranges::reverseβ reverses the order of elements in a range (C++20) (niebloid)reverse_copyβ creates a copy of a range that is reversed (function template)
cute sign-off: source http://cppreference.com, version/date 2024.06.10, manual page std::ranges...copy_result(3) β bye bestie, slay your ranges! π«Άπ
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms