C++ std::move() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<algorithm> · C++11

Definition

In C++, the algorithm std::move() moves each element of a range into a destination. Moving can be much cheaper than copying for big objects such as strings or vectors.

After the move, the source elements still exist but their values are unspecified. Do not rely on what they hold. Note the algorithm takes three arguments. The one-argument std::move(x) from <utility> is a different function.

Syntax

Add #include <algorithm>, then call std::move(). Start with these two lines.

C++
std::move(src.begin(), src.end(), dst.begin());
std::move(src.begin(), src.end(), std::back_inserter(dst));
CallWhat it does
std::move(src.begin(), src.end(), dst.begin());Move into a destination that already has enough elements.
std::move(src.begin(), src.end(), std::back_inserter(dst));Move and append to a vector.
Library declarations
C++
template<class InputIt, class OutputIt>
OutputIt move(InputIt first, InputIt last, OutputIt d_first);

Parameters

ParameterDescription
first, lastRequired. The source range.
d_firstRequired. Where the moved elements start in the destination.

Return Value

Return ValueDescription
IteratorPoints just after the last element written in the destination.

C++ Examples

Example 1 — Move strings

Move two strings into another vector.

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

int main() {
    std::vector<std::string> src = {"alpha", "beta"};
    std::vector<std::string> dst(2);

    std::move(src.begin(), src.end(), dst.begin());

    for (const std::string& x : dst) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

How It Works

  1. dst(2) creates two empty strings that will receive the moved values.
  2. Each string is moved, so its text is handed over without a full copy.
  3. The destination holds the original words. The source strings are now in an unspecified state.

Example 2 — Move and append

Add moved names to the end of a growing vector.

C++
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>

int main() {
    std::vector<std::string> a = {"Ann", "Bob"};
    std::vector<std::string> b;

    std::move(a.begin(), a.end(), std::back_inserter(b));

    std::cout << "b has " << b.size() << " names\n";
    for (const std::string& x : b) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

How It Works

  1. std::back_inserter(b) calls push_back() with a moved value each time.
  2. The vector b grows from zero to two elements.
  3. We print only b, because the old values in a are not safe to read.

Example 3 — Move unique_ptr

Some types cannot be copied, but they can be moved.

C++
#include <algorithm>
#include <iostream>
#include <memory>
#include <vector>

int main() {
    std::vector<std::unique_ptr<int>> from;
    from.push_back(std::make_unique<int>(5));
    from.push_back(std::make_unique<int>(8));
    std::vector<std::unique_ptr<int>> to(2);

    std::move(from.begin(), from.end(), to.begin());

    std::cout << *to[0] << ' ' << *to[1] << '\n';
    std::cout << std::boolalpha << "Source is null: " << (from[0] == nullptr) << '\n';
    return 0;
}

How It Works

  1. A unique_ptr owns its object and cannot be copied, so copy() would not compile.
  2. move() hands each pointer to the destination.
  3. A moved-from unique_ptr is guaranteed to be null, which is why the last line prints true.

Frequently Asked Questions

In C++, std::move() moves the elements of a range into another place instead of copying them. Include <algorithm> to use it.
Include <algorithm> for the three-argument range version. It needs C++11 or newer, so compile with -std=c++11 or later.
It returns an iterator to the position after the last moved element in the destination.
The three-argument version in <algorithm> moves a whole range. The one-argument version in <utility> only turns an object into something that can be moved from.
The elements are valid but their values are unspecified. You can assign new values to them or destroy them.

Next: move_backward()

Moves a range into a destination, starting from the last element.

move_backward() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful