本文主要是介绍c++11--std::move,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
move的实现原理
move对哪些数据有用
#include <iostream>
#include <vector>
#include <memory>int main()
{// intint a = 100;int b = std::move(a);std::cout << "a= " << a << std::endl;std::cout << "b= " << b << std::endl;// 小数double d1 = 12.34;double d2 = std::move(d1);std::cout << "d1= " << d1 << std::endl;std::cout << "d2= " << d2 << std::endl;// 普通指针int *c = new int(111);int *d = new int;d = std::move(c);std::cout << "c= " << *c << std::endl;std::cout << "d= " << *d << std::endl;// 对象指针std::vector<int> vec1{1, 2, 3, 4};std::vector<int> vec2 = std::move(vec1);if (!vec1.empty())std::cout << "vec1:" << vec1[0] << ", " << vec1[1] << ", " << vec1[2] << ", " << vec1[3] << ", " << std::endl;if (!vec2.empty())std::cout << "vec2:" << vec2[0] << ", " << vec2[1] << ", " << vec2[2] << ", " << vec2[3] << ", " << std::endl;// 对象std::string str1 = "aaa";std::string str2 = std::move(str1);if (!str1.empty()){std::cout << "str1=" << str1 << std::endl;}if (!str2.empty()){std::cout << "str2=" << str2 << std::endl;}// 共享指针auto pt1 = std::make_shared<int>(99);auto pt2 = std::move(pt1);if (pt1){std::cout << "pt1= " << *pt1 << std::endl;}if (pt2){std::cout << "pt2= " << *pt2 << std::endl;std::cout << "use_count= " << pt2.use_count() << std::endl;}return 0;
}
这篇关于c++11--std::move的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!