因为新项目需要有在多线程传递容器的操作,特意做了一个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;
}
2. 测试显示结果
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)