fork download
  1. #include <iostream>
  2. #include <utility>
  3. #include <vector>
  4. #include <string>
  5. int main()
  6. {
  7. std::string str = "Hello";
  8. std::vector<std::string> v;
  9. //调用常规的拷贝构造函数,新建字符数组,拷贝数据
  10. v.push_back(str);
  11. std::cout << "After copy, str is \"" << str << "\"\n";
  12. //调用移动构造函数,掏空str,掏空后,最好不要使用str
  13. v.push_back(std::move(str));
  14. std::cout << "After move, str is \"" << str << "\"\n";
  15. std::cout << "The contents of the vector are \"" << v[0]
  16. << "\", \"" << v[1] << "\"\n";
  17. }
Success #stdin #stdout 0s 4320KB
stdin
Standard input is empty
stdout
After copy, str is "Hello"
After move, str is ""
The contents of the vector are "Hello", "Hello"