Moving 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
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.
Moving transfers ownership of resources. Copying duplicates them.
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Fantastisk!
Completion rate forbedret til 10
Moving Standard Library Objects
Sveip for å vise menyen
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
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.
Moving transfers ownership of resources. Copying duplicates them.
Takk for tilbakemeldingene dine!