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.
std::move(src.begin(), src.end(), dst.begin()); // algorithm: three arguments auto&& r = std::move(x); // cast: one argument
Try it
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.
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
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 thestdnamespace, which makes the name confusing. - Move-only types: this is the way to transfer ranges of
unique_ptror other move-only objects, wherestd::copycannot 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
intand other trivially copyable types it behaves likecopy(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
eraseafterwards if you want fewer elements. - Ranges (C++20):
std::ranges::moveandmove_backwardaccept a range and return a result struct withinandout. - Complexity: exactly
last - firstmove assignments. - Related helper:
std::move_iteratorandstd::make_move_iteratormake any algorithm or constructor move instead of copy.