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.
Tack för dina kommentarer!
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
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?
Fantastiskt!
Completion betyg förbättrat till 10
Moving Standard Library Objects
Svep för att visa menyn
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.
Tack för dina kommentarer!