Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen 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

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 1

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

bookMoving Standard Library Objects

Swipe um das Menü anzuzeigen

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

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 1
some-alt