Namespaces
Variants

std:: rotate_copy

From cppreference.net
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
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
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
헤더 파일에 정의됨 <algorithm>
template < class ForwardIt, class OutputIt >

OutputIt rotate_copy ( ForwardIt first, ForwardIt middle,

ForwardIt last, OutputIt d_first ) ;
(1) (constexpr since C++20)
template < class ExecutionPolicy,

class ForwardIt1, class ForwardIt2 >
ForwardIt2 rotate_copy ( ExecutionPolicy && policy,
ForwardIt1 first, ForwardIt1 middle,

ForwardIt1 last, ForwardIt2 d_first ) ;
(2) (since C++17)

[ first , last ) 범위의 왼쪽 회전 d_first 로 복사합니다.

1) 범위 [ first , last ) 의 요소들을 d_first 에서 시작하는 대상 범위로 복사합니다. 이때 [ first , middle ) 의 요소들이 [ middle , last ) 의 요소들 뒤에 위치하며, 두 범위의 요소 순서는 원본 그대로 유지됩니다.
2) (1) 과 동일하지만, policy 에 따라 실행됩니다.
이 오버로드는 다음의 모든 조건이 만족될 때만 오버로드 해결에 참여합니다:

std:: is_execution_policy_v < std:: decay_t < ExecutionPolicy >> true 입니다.

(C++20 이전)

std:: is_execution_policy_v < std:: remove_cvref_t < ExecutionPolicy >> true 입니다.

(C++20 이후)

다음 조건 중 하나라도 충족되면, 동작은 정의되지 않습니다:

  • [ first , middle ) 또는 [ middle , last ) 가 유효한 범위가 아닙니다.
  • 소스와 대상 범위가 겹칩니다.

목차

매개변수

first, last - 복사할 원소들의 소스 범위 를 정의하는 반복자 쌍
middle - [ first , last ) 범위 내에 있으며 새로운 범위의 시작 부분에 나타나야 할 원소를 가리키는 반복자
d_first - 대상 범위의 시작 지점
policy - 사용할 실행 정책
타입 요구사항
-
ForwardIt, ForwardIt1, ForwardIt2 LegacyForwardIterator 요구사항을 충족해야 함
-
OutputIt LegacyOutputIterator 요구사항을 충족해야 함

반환값

복사된 마지막 요소의 다음 위치를 가리키는 출력 반복자입니다.

복잡도

정확히 std:: distance ( first, last ) 번의 할당이 수행됩니다.

예외

ExecutionPolicy 라는 템플릿 매개변수를 사용하는 오버로드는 다음과 같이 오류를 보고합니다:

  • 알고리즘의 일부로 호출된 함수 실행 중 예외가 발생하고 ExecutionPolicy 표준 정책 중 하나인 경우, std::terminate 가 호출됩니다. 다른 ExecutionPolicy 의 경우 동작은 구현에 따라 정의됩니다.
  • 알고리즘이 메모리 할당에 실패하는 경우, std::bad_alloc 이 throw됩니다.

가능한 구현

다음 구현체들도 참조하십시오: libstdc++ , libc++ , 그리고 MSVC STL .

template<class ForwardIt, class OutputIt>
constexpr // C++20부터
OutputIt rotate_copy(ForwardIt first, ForwardIt middle,
                     ForwardIt last, OutputIt d_first)
{
    d_first = std::copy(middle, last, d_first);
    return std::copy(first, middle, d_first);
}

예제

#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::find(src.begin(), src.end(), 3);
    std::rotate_copy(src.begin(), pivot, src.end(), dest.begin());
    for (int i : dest)
        std::cout << i << ' ';
    std::cout << '\n';
    // 회전 결과를 std::cout으로 직접 복사
    pivot = std::find(dest.begin(), dest.end(), 1);
    std::rotate_copy(dest.begin(), pivot, dest.end(),
                     std::ostream_iterator<int>(std::cout, " "));
    std::cout << '\n';
}

출력:

3 4 5 1 2
1 2 3 4 5

참고 항목

범위 내 요소들의 순서를 회전시킵니다
(함수 템플릿)
요소들의 범위를 복사하고 회전시킵니다
(알고리즘 함수 객체)