std:: move
|
헤더 파일에 정의됨
<algorithm>
|
||
|
template
<
class
InputIt,
class
OutputIt
>
OutputIt move
(
InputIt first, InputIt last,
|
(1) |
(C++11부터)
(C++20부터 constexpr) |
|
template
<
class
ExecutionPolicy,
class
ForwardIt1,
class
ForwardIt2
>
ForwardIt2 move
(
ExecutionPolicy
&&
policy,
|
(2) | (C++17부터) |
[
first
,
last
)
에 있는 요소들을
d_first
로 시작하는 다른 범위로 이동합니다. first부터 시작하여 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 이후) |
만약
d_first
가 범위
[
first
,
last
)
내에 있을 경우, 동작은 정의되지 않습니다. 이 경우
std::move_backward
를 대신 사용할 수 있습니다.
목차 |
매개변수
| first, last | - | 이동할 요소들의 소스 범위 를 정의하는 반복자 쌍 |
| d_first | - | 대상 범위의 시작 지점 |
| policy | - | 사용할 실행 정책 |
| 타입 요구사항 | ||
-
InputIt
는
LegacyInputIterator
요구사항을 충족해야 함
|
||
-
OutputIt
는
LegacyOutputIterator
요구사항을 충족해야 함
|
||
-
ForwardIt1, ForwardIt2
는
LegacyForwardIterator
요구사항을 충족해야 함
|
||
반환값
이동된 마지막 요소의 다음 요소를 가리키는 반복자.
복잡도
정확히 std:: distance ( first, last ) 번의 이동 할당.
예외
ExecutionPolicy
라는 템플릿 매개변수를 사용하는 오버로드는 다음과 같이 오류를 보고합니다:
-
알고리즘의 일부로 호출된 함수 실행 중 예외가 발생하고
ExecutionPolicy가 표준 정책 중 하나인 경우, std::terminate 가 호출됩니다. 다른ExecutionPolicy의 경우 동작은 구현에 따라 정의됩니다. - 알고리즘이 메모리 할당에 실패하는 경우, std::bad_alloc 이 throw됩니다.
가능한 구현
template<class InputIt, class OutputIt> OutputIt move(InputIt first, InputIt last, OutputIt d_first) { for (; first != last; ++d_first, ++first) *d_first = std::move(*first); return d_first; } |
참고 사항
겹치는 범위를 이동할 때,
std::move
는 왼쪽으로 이동할 때(대상 범위의 시작이 원본 범위 밖에 있을 때) 적절하며,
std::move_backward
는 오른쪽으로 이동할 때(대상 범위의 끝이 원본 범위 밖에 있을 때) 적절합니다.
예제
다음 코드는 스레드 객체(자체적으로 복사할 수 없는)를 한 컨테이너에서 다른 컨테이너로 이동합니다.
#include <algorithm> #include <chrono> #include <iostream> #include <iterator> #include <list> #include <thread> #include <vector> void f(int n) { std::this_thread::sleep_for(std::chrono::seconds(n)); std::cout << "thread " << n << " ended" << std::endl; } int main() { std::vector<std::jthread> v; v.emplace_back(f, 1); v.emplace_back(f, 2); v.emplace_back(f, 3); std::list<std::jthread> l; // copy()는 컴파일되지 않습니다. std::jthread가 복사 불가능하기 때문입니다 std::move(v.begin(), v.end(), std::back_inserter(l)); }
출력:
thread 1 ended thread 2 ended thread 3 ended
참고 항목
|
(C++11)
|
요소 범위를 역순으로 새 위치로 이동합니다
(함수 템플릿) |
|
(C++11)
|
인수를 xvalue로 변환합니다
(함수 템플릿) |
|
(C++20)
|
요소 범위를 새 위치로 이동합니다
(알고리즘 함수 객체) |