algorithm_move

algorithm_move (이동 복사)

std::move는 소스 범위 [first, last)의 모든 요소를 목적지 범위로 이동(move)해요. 복사 대신 이동 생성자를 활용해요.

출처: cppreference

본문

std::move(알고리즘)는 소스 범위 [first, last)의 모든 요소를 목적지 범위 [d_first, std::next(d_first, std::distance(first, last)))로 이동해요. <algorithm> 헤더에 정의되어 있어요. <utility>std::move(형변환 함수)와는 다르니 주의하세요.

template< class InputIt, class OutputIt >
OutputIt move( InputIt first, InputIt last, OutputIt d_first );
  • 1번 오버로드first부터 last까지 순서대로 이동해요. d_first가 소스 범위 안에 있으면 동작이 정의되지 않아요.
  • 2번 오버로드(병렬) — 이동 순서가 policy에 따라 결정돼요.

반환값 (Return value)

목적지 범위의 끝(past-the-end) 반복자예요.

복잡도 (Complexity)

Nstd::distance(first, last)라고 하면 정확히 N번의 이동 배정이 필요해요.

예제 (Example)

#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>

int main()
{
    std::vector<std::string> source{"one", "two", "three"};
    std::vector<std::string> dest;

    std::move(source.begin(), source.end(), std::back_inserter(dest));
    std::cout << "dest: ";
    for (auto& s : dest) std::cout << s << ' ';
    std::cout << '\n';
}

더 알아보기 (Learn more)

cppreference