std:: rotate
|
헤더 파일에 정의됨
<algorithm>
|
||
|
template
<
class
ForwardIt
>
ForwardIt rotate ( ForwardIt first, ForwardIt middle, ForwardIt last ) ; |
(1) | (constexpr since C++20) |
|
template
<
class
ExecutionPolicy,
class
ForwardIt
>
ForwardIt rotate
(
ExecutionPolicy
&&
policy,
|
(2) | (since C++17) |
std::rotate
는 범위
[
first
,
last
)
내의 요소들을 다음과 같은 방식으로 교환합니다:
[
first
,
middle
)
범위의 요소들이
[
middle
,
last
)
범위의 요소들 뒤에 위치하도록 하되, 두 범위 내 요소들의 순서는 유지됩니다.
|
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)가 유효한 범위 가 아닙니다.
|
(C++11 이전) |
|
(C++11 이후) |
목차 |
매개변수
| first, last | - | 회전할 요소들의 범위 를 정의하는 반복자 쌍 |
| middle | - | 회전된 범위의 시작 부분에 나타나야 할 요소 |
| policy | - | 사용할 실행 정책 |
| 타입 요구사항 | ||
-
ForwardIt
는
LegacyForwardIterator
의 요구사항을 충족해야 합니다.
|
||
반환값
* first 가 원래 참조하던 요소에 대한 반복자, 즉 std:: distance ( middle, last ) 번째 first 의 다음 반복자입니다.
복잡도
최대 std:: distance ( first, last ) 번의 교환.
예외
ExecutionPolicy
라는 템플릿 매개변수를 사용하는 오버로드는 다음과 같이 오류를 보고합니다:
-
알고리즘의 일부로 호출된 함수 실행 중 예외가 발생하고
ExecutionPolicy가 표준 정책 중 하나인 경우, std::terminate 가 호출됩니다. 다른ExecutionPolicy의 경우 동작은 구현에 따라 정의됩니다. - 알고리즘이 메모리 할당에 실패하는 경우, std::bad_alloc 이 throw됩니다.
가능한 구현
다음 구현도 참조하십시오: libstdc++ , libc++ , 그리고 MSVC STL .
template<class ForwardIt> constexpr // since C++20 ForwardIt rotate(ForwardIt first, ForwardIt middle, ForwardIt last) { if (first == middle) return last; if (middle == last) return first; ForwardIt write = first; ForwardIt next_read = first; // "read"가 "last"에 도달할 때의 읽기 위치 for (ForwardIt read = middle; read != last; ++write, ++read) { if (write == next_read) next_read = read; // "first"가 이동한 위치 추적 std::iter_swap(write, read); } // 남은 시퀀스를 제자리에서 회전 rotate(write, next_read, last); return write; } |
참고 사항
std::rotate
는
ForwardIt
가
LegacyBidirectionalIterator
또는 (더 나은 경우)
LegacyRandomAccessIterator
를 만족할 경우 일반적인 구현에서 더 나은 효율성을 가집니다.
구현체들(예:
MSVC STL
)은 반복자 타입이
LegacyContiguousIterator
를 만족하고 그 값 타입의 스왑 작업이 비트리비얼 특수 멤버 함수를 호출하지 않으며
ADL
에서 발견된
swap
을 호출하지 않을 때 벡터화를 활성화할 수 있습니다.
예제
std::rotate
는 많은 알고리즘에서 공통적으로 사용되는 구성 요소입니다. 이 예제는
insertion sort
를 보여줍니다.
#include <algorithm> #include <iostream> #include <vector> auto print = [](const auto remark, const auto& v) { std::cout << remark; for (auto n : v) std::cout << n << ' '; std::cout << '\n'; }; int main() { std::vector<int> v{2, 4, 2, 0, 5, 10, 7, 3, 7, 1}; print("before sort:\t\t", v); // insertion sort for (auto i = v.begin(); i != v.end(); ++i) std::rotate(std::upper_bound(v.begin(), i, *i), i, i + 1); print("after sort:\t\t", v); // simple rotation to the left std::rotate(v.begin(), v.begin() + 1, v.end()); print("simple rotate left:\t", v); // simple rotation to the right std::rotate(v.rbegin(), v.rbegin() + 1, v.rend()); print("simple rotate right:\t", v); }
출력:
before sort: 2 4 2 0 5 10 7 3 7 1 after sort: 0 1 2 2 3 4 5 7 7 10 simple rotate left: 1 2 2 3 4 5 7 7 10 0 simple rotate right: 0 1 2 2 3 4 5 7 7 10
결함 보고서
다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 488 | C++98 | first 가 가리키는 요소의 새로운 위치가 반환되지 않았음 | 반환됨 |
참고 항목
|
요소 범위를 복사하고 회전시킵니다
(함수 템플릿) |
|
|
(C++20)
|
범위 내 요소들의 순서를 회전시킵니다
(알고리즘 함수 객체) |