Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ranges::rotate_copy,std::ranges::rotate_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::forward_iterator I,std::sentinel_for<I> S,

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

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

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

          rotate_copy( R&& r,ranges::iterator_t<R> middle, O result);
      (2)(since C++20)
      Helper types
      template<class I,class O>
      using rotate_copy_result= in_out_result<I, O>;
      (3)(since C++20)

      Copies theleft rotation of[firstlast) toresult.

      1) Copies the elements from the source range[firstlast), such that in the destination range, the elements in[firstmiddle) are placed after the elements in[middlelast) while the orders of the elements in both ranges are preserved.
      The behavior is undefined if either[firstmiddle) or[middlelast) is not a valid range, or 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 from
      r - the source range of elements to copy from
      middle - the iterator to the element that should appear at the beginning of the destination range
      result - beginning of the destination range

      [edit]Return value

      {last, result+ N}, whereN=ranges::distance(first, last).

      [edit]Complexity

      Linear: exactlyN assignments.

      [edit]Notes

      If the value type isTriviallyCopyable and the iterator types satisfycontiguous_iterator, implementations ofranges::rotate_copy usually avoid multiple assignments by using a "bulk copy" function such asstd::memmove.

      [edit]Possible implementation

      See also the implementations inlibstdc++ andMSVC STL.

      struct rotate_copy_fn{template<std::forward_iterator I,std::sentinel_for<I> S,std::weakly_incrementable O>    requiresstd::indirectly_copyable<I, O>constexpr ranges::rotate_copy_result<I, O>        operator()(I first, I middle, S last, O result)const{auto c1{ranges::copy(middle, std::move(last), std::move(result))};auto c2{ranges::copy(std::move(first), std::move(middle), std::move(c1.out))};return{std::move(c1.in), std::move(c2.out)};} template<ranges::forward_range R,std::weakly_incrementable O>    requiresstd::indirectly_copyable<ranges::iterator_t<R>, O>constexpr ranges::rotate_copy_result<ranges::borrowed_iterator_t<R>, O>        operator()(R&& r,ranges::iterator_t<R> middle, O result)const{return(*this)(ranges::begin(r), std::move(middle),ranges::end(r), std::move(result));}}; inlineconstexpr rotate_copy_fn rotate_copy{};

      [edit]Example

      Run this code
      #include <algorithm>#include <iostream>#include <iterator>#include <vector> int main(){std::vector<int> src{1,2,3,4,5};std::vector<int> dest(src.size());auto pivot= std::ranges::find(src,3);     std::ranges::rotate_copy(src, pivot, dest.begin());for(int i: dest)std::cout<< i<<' ';std::cout<<'\n'; // copy the rotation result directly to the std::cout    pivot= std::ranges::find(dest,1);    std::ranges::rotate_copy(dest, pivot,std::ostream_iterator<int>(std::cout," "));std::cout<<'\n';}

      Output:

      3 4 5 1 21 2 3 4 5

      [edit]See also

      rotates the order of elements in a range
      (algorithm function object)[edit]
      copies a range of elements to a new location
      (algorithm function object)[edit]
      copies and rotate a range of elements
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/algorithm/ranges/rotate_copy&oldid=181153"

      [8]ページ先頭

      ©2009-2025 Movatter.jp