首页 > 其他 > 详细

std::move()

时间:2015-11-20 00:04:58      阅读:186      评论:0      收藏:0      [点我收藏+]
 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     // uses the push_back(const T&) overload, which means
11     // we‘ll incur the cost of copying str
12     v.push_back(str);
13     std::cout << "After copy, str is \"" << str << "\"\n";
14  
15     // uses the rvalue reference push_back(T&&) overload,
16     // which means no strings will copied; instead, the contents
17     // of str will be moved into the vector.  This is less
18     // expensive, but also means str might now be empty.
19     v.push_back(std::move(str));
20     std::cout << "After move, str is \"" << str << "\"\n";
21  
22     std::cout << "The contents of the vector are \"" << v[0]
23                                          << "\", \"" << v[1] << "\"\n";
24 }

最好配合着c++标准库一起看。

std::move()

原文:http://www.cnblogs.com/cdwodm/p/4979242.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!