algorithm_move_backward

algorithm_move_backward (역방향 이동 복사)

std::move_backward는 소스 범위 [first, last)의 요소들을 역순으로 이동해서 d_last에서 끝나는 목적지 범위에 넣어요. 오른쪽으로 이동할 때 적합한 함수예요.

출처: cppreference

본문

std::move_backward는 소스 범위 [first, last)의 요소들을 d_last에서 끝나는 목적지 범위로, last부터 first까지 역순으로 이동해요. <algorithm> 헤더에 정의되어 있어요.

template< class BidirIt1, class BidirIt2 >
BidirIt2 move_backward( BidirIt1 first, BidirIt1 last, BidirIt2 d_last );

반환값 (Return value)

목적지 범위에서 마지막으로 이동 배정된 요소를 가리키는 반복자예요. 이동된 요소가 없으면 d_last를 돌려줘요.

복잡도 (Complexity)

정확히 std::distance(first, last)번의 이동 배정이 필요해요.

참고 (Notes)

겹치는 범위를 처리할 때, 오른쪽으로 이동(목적지 끝이 소스 범위 밖)하면 std::move_backward, 왼쪽으로 이동하면 std::move가 적합해요.

예제 (Example)

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

int main()
{
    std::vector<std::string> src{"a", "b", "c"};
    std::vector<std::string> dst(5);

    std::move_backward(src.begin(), src.end(), dst.end());
    for (auto& s : dst) std::cout << s << ' ';
    std::cout << '\n';
}

더 알아보기 (Learn more)

cppreference