C/C++ Arena

Moving instead of copying

copy gets its own new heap buffer with the same numbers: that's the cost of copying. moved instead takes over original's buffer: watch the heap, no new block appears. Afterwards original is left empty (valid, but with nothing in it).

#include <iostream>
#include <utility>
#include <vector>

int main() {
    std::vector<int> original{1, 2, 3};
    std::vector<int> copy = original;
    std::vector<int> moved = std::move(original);
    std::cout << copy.size() << " " << moved.size() << " " << original.size() << "\n";
    return 0;
}

Output:

3 3 0

From the lesson: Move semantics