Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ranges::reverse_copy,std::ranges::reverse_copy_result

      From cppreference.com
      <cpp‎ |algorithm‎ |ranges
       
       
      Algorithm library
      Constrained algorithms and algorithms on ranges(C++20)
      Constrained algorithms, e.g.ranges::copy,ranges::sort, ...
      Execution policies(C++17)
      Sorting and related operations
      Partitioning operations
      Sorting operations
      Binary search operations
      (on partitioned ranges)
      Set operations (on sorted ranges)
      Merge operations (on sorted ranges)
      Heap operations
      Minimum/maximum operations
      (C++11)
      (C++17)
      Lexicographical comparison operations
      Permutation operations
      C library
      Numeric operations
      Operations on uninitialized memory
       
      Constrained algorithms
      All names in this menu belong to namespacestd::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
      template<std::bidirectional_iterator I,std::sentinel_for<I> S,

               std::weakly_incrementable O>
      requiresstd::indirectly_copyable<I, O>
      constexpr reverse_copy_result<I, O>

          reverse_copy( I first, S last, O result);
      (1)(since C++20)
      template<ranges::bidirectional_range R,std::weakly_incrementable O>

      requiresstd::indirectly_copyable<ranges::iterator_t<R>, O>
      constexpr reverse_copy_result<ranges::borrowed_iterator_t<R>, O>

          reverse_copy( R&& r, O result);
      (2)(since C++20)
      Helper types
      template<class I,class O>
      using reverse_copy_result=ranges::in_out_result<I, O>;
      (3)(since C++20)
      1) Copies the elements from the source range[firstlast) to the destination range[resultresult+ N), whereN isranges::distance(first, last), in such a way that the elements in the new range are in reverse order. Behaves as if by executing the assignment*(result+ N-1- i)=*(first+ i) once for each integeri in[0N). The behavior is undefined if the source and destination ranges overlap.
      2) Same as(1), but usesr as the source range, as if usingranges::begin(r) asfirst andranges::end(r) aslast.

      The function-like entities described on this page arealgorithm function objects (informally known asniebloids), that is:

      Contents

      [edit]Parameters

      first, last - the iterator-sentinel pair defining the sourcerange of elements to copy
      r - the source range of elements to copy
      result - the beginning of the destination range.

      [edit]Return value

      {last, result+ N}.

      [edit]Complexity

      ExactlyN assignments.

      [edit]Notes

      Implementations (e.g.MSVC STL) may enable vectorization when the both iterator types modelcontiguous_iterator and have the same value type, and the value type isTriviallyCopyable.

      [edit]Possible implementation

      See also the implementations inMSVC STL andlibstdc++.

      struct reverse_copy_fn{template<std::bidirectional_iterator I,std::sentinel_for<I> S,std::weakly_incrementable O>    requiresstd::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>    requiresstd::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));}}; inlineconstexpr reverse_copy_fn reverse_copy{};

      [edit]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

      [edit]See also

      reverses the order of elements in a range
      (algorithm function object)[edit]
      creates a copy of a range that is reversed
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/algorithm/ranges/reverse_copy&oldid=180521"

      [8]ページ先頭

      ©2009-2025 Movatter.jp