因为新项目需要有在多线程传递容器的操作,特意做了一个vector容器move语义的测试代码。
1. 代码
#include <iostream>
#include <vector>
int main()
{
std::cout << "Hello World!\n";
std::vector<int> v0;
for (int i = 0; i < 10; i++)
v0.push_back(i);
std::vector<int> v1(std::move(v0));
std::cout << "v0: ";
for (auto it : v0)
std::cout << "," << it;
std::cout << std::endl;
std::cout << "v1: ";
for (auto it : v1)
std::cout << "," << it;
std::cout << std::endl;
std::cout << "~~move again~~" << std::endl;
v0 = std::move(v1);
std::cout << "v0: ";
for (auto it : v0)
std::cout << "," << it;
std::cout << std::endl;
std::cout << "v1: ";
for (auto it : v1)
std::cout << "," << it;
std::cout << std::endl;
return 0;
}