Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Algorithms

The std::move algorithm (range overload)

Reference for the three-argument std::move and move_backward algorithms: move elements between ranges, what is left behind and how it differs from the cast.

std::move has two unrelated meanings. With one argument it is the cast from <utility> that turns an lvalue into an rvalue reference (see move semantics). With three arguments, std::move(first, last, d_first) from <algorithm>, it is an algorithm: it moves each element of a range into the destination, like std::copy but using move assignment.

After the call the source elements are in a valid but unspecified state. For std::string or std::vector they are usually empty, but you must not rely on that. std::move_backward is the right-to-left variant for overlapping ranges.

C++
std::move(src.begin(), src.end(), dst.begin());   // algorithm: three arguments
auto&& r = std::move(x);                          // cast: one argument

Try it

C++ Moving elements between vectors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ move_backward and unique_ptr

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
template<class InIt, class OutIt>
OutIt move(InIt first, InIt last, OutIt d_first);          // <algorithm>

template<class BidIt1, class BidIt2>
BidIt2 move_backward(BidIt1 first, BidIt1 last, BidIt2 d_last);
Name Description
first, last Source range [first, last).
d_first Where the first moved element is written.
d_last End of the destination for move_backward.

Returns: an iterator one past the last element written (move_backward: the start of the written block).

Errors / undefined behavior: d_first must not lie inside [first, last) (use move_backward for that case); the destination must have room. Using a moved-from element other than to assign or destroy it relies on the type's guarantees.

Usage notes

  • Which std::move? The compiler picks by argument count: one argument is the cast, three are this algorithm. Both live in the std namespace, which makes the name confusing.
  • Move-only types: this is the way to transfer ranges of unique_ptr or other move-only objects, where std::copy cannot compile.
  • Moved-from state: each source element stays valid and destructible but its value is unspecified. The standard library types leave the value unspecified, even if in practice it is empty.
  • Trivial types: for int and other trivially copyable types it behaves like copy (and is just as fast).
  • Whole containers: moving all elements into another vector is simply dst = std::move(src), which steals the buffer in O(1).
  • Not the same as erase: moving does not shrink the source; call erase afterwards if you want fewer elements.
  • Ranges (C++20): std::ranges::move and move_backward accept a range and return a result struct with in and out.
  • Complexity: exactly last - first move assignments.
  • Related helper: std::move_iterator and std::make_move_iterator make any algorithm or constructor move instead of copy.