Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Moving Standard Library Objects | Applied Move Semantics
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C++ Move Semantics

bookMoving Standard Library Objects

Standard library containers such as std::vector and std::string use move semantics to make operations like resizing, inserting, or returning objects by value more efficient. Moving an object transfers ownership of its internal resources instead of duplicating them, avoiding costly deep copies of large data buffers. This leads to substantial performance gains when handling large datasets or strings.

main.cpp

main.cpp

copy
123456789101112131415161718192021222324
#include <iostream> #include <vector> #include <string> #include <chrono> int main() { auto timeit = [](auto&& f) { auto s = std::chrono::high_resolution_clock::now(); f(); return std::chrono::duration_cast<std::chrono::milliseconds>( std::chrono::high_resolution_clock::now() - s).count(); }; std::vector<int> v(1'000'000, 42); auto copyTime = timeit([&]{ std::vector<int> c = v; }); auto moveTime = timeit([&]{ std::vector<int> m = std::move(v); }); std::cout << "Vector copy: " << copyTime << " ms\nVector move: " << moveTime << " ms\n"; std::string s(1'000'000, 'x'); auto copyStr = timeit([&]{ std::string c = s; }); auto moveStr = timeit([&]{ std::string m = std::move(s); }); std::cout << "String copy: " << copyStr << " ms\nString move: " << moveStr << " ms\n"; }

When you move a std::vector or std::string, ownership of the internal data buffer is transferred to the new object instead of being copied. This eliminates the need for a deep copy, making moving operations much fasterβ€”especially for large data structures.

After a move, the source object remains valid but unspecified: you can safely destroy it or reassign a new value, but you should not access its previous contents.

Note
Note

Moving transfers ownership of resources. Copying duplicates them.

question mark

Why is moving a std::vector or std::string generally more efficient than copying?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 1

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain what "valid but unspecified" means in more detail?

What are some common mistakes to avoid when using move semantics?

Can you give an example of moving a std::vector in code?

bookMoving Standard Library Objects

Swipe to show menu

Standard library containers such as std::vector and std::string use move semantics to make operations like resizing, inserting, or returning objects by value more efficient. Moving an object transfers ownership of its internal resources instead of duplicating them, avoiding costly deep copies of large data buffers. This leads to substantial performance gains when handling large datasets or strings.

main.cpp

main.cpp

copy
123456789101112131415161718192021222324
#include <iostream> #include <vector> #include <string> #include <chrono> int main() { auto timeit = [](auto&& f) { auto s = std::chrono::high_resolution_clock::now(); f(); return std::chrono::duration_cast<std::chrono::milliseconds>( std::chrono::high_resolution_clock::now() - s).count(); }; std::vector<int> v(1'000'000, 42); auto copyTime = timeit([&]{ std::vector<int> c = v; }); auto moveTime = timeit([&]{ std::vector<int> m = std::move(v); }); std::cout << "Vector copy: " << copyTime << " ms\nVector move: " << moveTime << " ms\n"; std::string s(1'000'000, 'x'); auto copyStr = timeit([&]{ std::string c = s; }); auto moveStr = timeit([&]{ std::string m = std::move(s); }); std::cout << "String copy: " << copyStr << " ms\nString move: " << moveStr << " ms\n"; }

When you move a std::vector or std::string, ownership of the internal data buffer is transferred to the new object instead of being copied. This eliminates the need for a deep copy, making moving operations much fasterβ€”especially for large data structures.

After a move, the source object remains valid but unspecified: you can safely destroy it or reassign a new value, but you should not access its previous contents.

Note
Note

Moving transfers ownership of resources. Copying duplicates them.

question mark

Why is moving a std::vector or std::string generally more efficient than copying?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 1
some-alt