Move Semantics with std::move
Move semantics allow resources (like heap memory) to be moved from one object to another instead of being copied. std::move casts an object to an rvalue, enabling the move constructor to take over the data efficiently.
#include <iostream>#include <vector>#include <utility>int main() {std::vector<int> vec1 = {1, 2, 3, 4, 5};// Transfer ownership of resources from vec1 to vec2std::vector<int> vec2 = std::move(vec1);std::cout << "vec2 size: " << vec2.size() << std::endl;std::cout << "vec1 size: " << vec1.size() << std::endl; // Likely 0return 0;}