Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Moving Standard Library Objects | Applied Move Semantics
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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 1

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

bookMoving Standard Library Objects

Pyyhkäise näyttääksesi valikon

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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 1
some-alt