Namespaces
Variants
Views
Actions

std::ranges::rotate_copy

From cppreference.com
< cpp‎ | algorithm‎ | ranges
Revision as of 21:09, 29 November 2020 by Space Mission (Talk | contribs)

 
 
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy, ranges::sort, ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
(C++11)                (C++11)(C++11)

Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17)(C++11)
(C++20)(C++20)
Sampling operations
(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 namespace 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
Numeric operations
(C++23)            
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>

  requires std::indirectly_copyable<I, O>
  constexpr ranges::rotate_copy_result<I, O>

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

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

    ranges::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)
1) Copies the elements from the source range [first, last), to the destination range beginning at result in such a way, that the element middle becomes the first element of the destination range and middle - 1 becomes the last element.
Effect: the destination range contains a left rotated copy of the source range.
Preconditions of this function: [first, middle) and [middle, last) are valid ranges, and the source and destination ranges do not overlap.
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:

In practice, they may be implemented as function objects, or with special compiler extensions.

Contents

Parameters

first, last - the source range of elements to copy from
r - the source range of elements to copy from
middle - the element that should appear at the beginning of the destination range
result - beginning of the destination range

Return value

1) ranges::rotate_copy_result<I, O>{last, result + N}, where N = {last - first}.
2) Same as (1) but the return type is ranges::rotate_copy_result<ranges::borrowed_iterator_t<R>, O>.

Complexity

Linear: exactly last - first assignments.

Notes

In practice, implementations of std::ranges::rotate_copy avoid multiple assignments and use bulk copy functions such as std::memmove if the value type is TriviallyCopyable and the iterator types satisfy contiguous_iterator.

Possible implementation

struct rotate_copy_fn {
  template<std::forward_iterator I, std::sentinel_for<I> S, std::weakly_incrementable O>
    requires std::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>
    requires std::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));
      }
};
 
inline constexpr rotate_copy_fn rotate_copy{};

Example

#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 (const auto &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 2
1 2 3 4 5

See also

rotates the order of elements in a range
(niebloid)[edit]
copies a range of elements to a new location
(niebloid)[edit]
rotates the order of elements in a range
(function template) [edit]