std::move_backward

From cppreference.com
< cpp‎ | algorithm
 
 
Algorithm library
Execution policies (C++17)
Non-modifying sequence operations
(C++11)(C++11)(C++11)
(C++17)
Modifying sequence operations
(C++11)
(C++11)
move_backward
(C++11)
(C++20)(C++20)
Operations on uninitialized storage
Partitioning operations
Sorting operations
(C++11)
Binary search operations
Set operations (on sorted ranges)
Heap operations
(C++11)
Minimum/maximum operations
(C++11)
(C++17)
Permutations
Numeric operations
C library
 
Defined in header <algorithm>
template< class BidirIt1, class BidirIt2 >
BidirIt2 move_backward( BidirIt1 first, BidirIt1 last, BidirIt2 d_last );
(since C++11)
(until C++20)
template< class BidirIt1, class BidirIt2 >
constexpr BidirIt2 move_backward( BidirIt1 first, BidirIt1 last, BidirIt2 d_last );
(since C++20)

Moves the elements from the range [first, last), to another range ending at d_last. The elements are moved in reverse order (the last element is moved first), but their relative order is preserved.

The behavior is undefined if d_last is within (first, last]. std::move must be used instead of std::move_backward in that case.

Parameters

first, last - the range of the elements to move
d_last - end of the destination range
Type requirements
-
BidirIt1, BidirIt2 must meet the requirements of LegacyBidirectionalIterator.

Return value

Iterator in the destination range, pointing at the last element moved.

Complexity

Exactly last - first move assignments.

Possible implementation

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;
}

Notes

When moving overlapping ranges, std::move is appropriate when moving to the left (beginning of the destination range is outside the source range) while std::move_backward is appropriate when moving to the right (end of the destination range is outside the source range).

Example

#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
 
int main()
{
    std::vector<std::string> src{"foo", "bar", "baz"};
    std::vector<std::string> dest(src.size());
 
    std::cout << "src: ";
    for (const auto &s : src)
    {
        std::cout << s << ' ';
    }   
    std::cout << "\ndest: ";
    for (const auto &s : dest)
    {
        std::cout << s << ' ';
    }   
    std::cout << '\n';
 
    std::move_backward(src.begin(), src.end(), dest.end());
 
    std::cout << "src: ";                                                       
    for (const auto &s : src)
    {
        std::cout << s << ' ';
    }   
    std::cout << "\ndest: ";
    for (const auto &s : dest)
    {
        std::cout << s << ' ';
    }   
    std::cout << '\n';
}

Output:

src: foo bar baz 
dest:    
src:    
dest: foo bar baz

See also

(C++11)
moves a range of elements to a new location
(function template)