Namespaces
Variants

std:: move_backward

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)
move_backward
(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 BidirIt1, class BidirIt2 >
BidirIt2 move_backward ( BidirIt1 first, BidirIt1 last, BidirIt2 d_last ) ;
(C++11부터)
(constexpr since C++20)

범위 [ first , last ) 에 있는 요소들을 d_last 로 끝나는 다른 범위로 이동합니다. 요소들은 역순으로(마지막 요소가 먼저 이동됨) 이동되지만, 상대적 순서는 유지됩니다.

만약 d_last ( first , last ] 범위 내에 있다면, 동작은 정의되지 않습니다. 이 경우 std::move 를 대신 사용할 수 있습니다.

목차

매개변수

first, last - 이동할 요소들의 소스 범위 를 정의하는 반복자 쌍
d_last - 대상 범위의 끝
타입 요구사항
-
BidirIt1, BidirIt2 LegacyBidirectionalIterator 요구사항을 충족해야 합니다.

반환값

대상 범위의 반복자로, 이동된 마지막 요소를 가리킵니다.

복잡도

정확히 std:: distance ( first, last ) 번의 이동 할당.

가능한 구현

template<class BidirIt1, class BidirIt2>
BidirIt2 move_backward(BidirIt1 first, BidirIt1 last, BidirIt2 d_last)
{
    while (first != last)
        *(--d_last) = std::move(*(--last));
    return d_last;
}

참고 사항

겹치는 범위를 이동할 때, std::move 는 왼쪽으로 이동할 때(대상 범위의 시작점이 소스 범위 밖에 있을 때) 적합한 반면, std::move_backward 는 오른쪽으로 이동할 때(대상 범위의 끝점이 소스 범위 밖에 있을 때) 적합합니다.

예제

#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <string_view>
#include <vector>
using container = std::vector<std::string>;
void print(std::string_view comment, const container& src, const container& dst = {})
{
    auto prn = [](std::string_view name, const container& cont)
    {
        std::cout << name;
        for (const auto &s : cont)
            std::cout << (s.empty() ? "∙" : s.data()) << ' ';
        std::cout << '\n';
    };
    std::cout << comment << '\n';
    prn("src: ", src);
    if (dst.empty())
        return;
    prn("dst: ", dst);
}
int main()
{
    container src{"foo", "bar", "baz"};
    container dst{"qux", "quux", "quuz", "corge"};
    print("Non-overlapping case; before move_backward:", src, dst);
    std::move_backward(src.begin(), src.end(), dst.end());
    print("After:", src, dst);
    src = {"snap", "crackle", "pop", "lock", "drop"};
    print("Overlapping case; before move_backward:", src);
    std::move_backward(src.begin(), std::next(src.begin(), 3), src.end());
    print("After:", src);
}

출력:

Non-overlapping case; before move_backward:
src: foo bar baz
dst: qux quux quuz corge
After:
src: ∙ ∙ ∙
dst: qux foo bar baz
Overlapping case; before move_backward:
src: snap crackle pop lock drop
After:
src: ∙ ∙ snap crackle pop

참고 항목

(C++11)
요소들의 범위를 새로운 위치로 이동시킵니다
(함수 템플릿)
요소들의 범위를 역순으로 새로운 위치로 이동시킵니다
(알고리즘 함수 객체)