A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://en.cppreference.com/w/cpp/algorithm/../symbol_index/../algorithm/ranges/rotate_copy.html below:

std::ranges::rotate_copy, std::ranges::rotate_copy_result - cppreference.com

Call signature

(1) (since C++20) (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 the left rotation of [firstlast) to result.

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.

The function-like entities described on this page are algorithm function objects (informally known as niebloids), that is:

[edit] Parameters first, last - the iterator-sentinel pair defining the source range 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}, where N = ranges::distance(first, last).

[edit] Complexity

Linear: exactly N assignments.

[edit] Notes

If the value type is TriviallyCopyable and the iterator types satisfy contiguous_iterator, implementations of ranges::rotate_copy usually avoid multiple assignments by using a "bulk copy" function such as std::memmove.

[edit] Possible implementation

See also the implementations in libstdc++ and MSVC STL.

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 {};
[edit] 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 (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:

[edit] See also

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4