首页 / C++ / c++ 右值以及move
c++ 右值以及move
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c++ 右值以及move,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1986字,纯文字阅读大概需要3分钟。
内容图文
![c++ 右值以及move](/upload/InfoBanner/zyjiaocheng/757/f8d512ab3ab14291984730a23c1b28e4.jpg)
之前对move一直存在误解,想得太万能了。
右值实际上就是临时内存,类似于(1+2),可以int &&number = 1+2;
move的话是将参数变成自身类型的右值,然后赋值给其他变量。
我一开始以为move是交换两块内存的指针之类,复杂度是O(1)那种,后来测试了一下发现并不是这样。测试代码如下:
![c++ 右值以及move - 文章图片](/upload/getfiles/0001/2021/5/4/20210504051722333.jpg)
![c++ 右值以及move - 文章图片](/upload/getfiles/0001/2021/5/4/20210504051722447.jpg)
#include <iostream> #include <string> #include <utility> #include <cstring> #include <sys/time.h> using namespace std; uint64_t getCurrentTime() { struct timeval tv; gettimeofday(&tv,NULL); return (uint64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; } struct node { char str[20000000]; }; inline void testMove(node *node1, node *node2) { uint64_t startTime = getCurrentTime(); (*node2) = move(*node1); uint64_t endTime = getCurrentTime(); cout << "cost " << endTime - startTime << " us" << endl; } inline void testCopy(node *node1, node *node2) { uint64_t startTime = getCurrentTime(); (*node2) = (*node1); uint64_t endTime = getCurrentTime(); cout << "cost " << endTime - startTime << " us" << endl; } int main() { const uint32_t str1Len = 25000000, str2Len = 30000000; char *str1 = new char[str1Len], *str2 = new char[str2Len]; memset(str1, 'a', str1Len); memset(str2, 'b', str2Len); node *node1 = (node *)str1; node *node2 = (node *)str2; testMove(node1, node2); testCopy(node1, node2); #ifdef DEBUG_OUTPUT for (int i = 0; i < str1Len; i++) { cout << (char)str1[i] << " "; } cout << endl; for (int i = 0; i < str2Len; i++) { cout << (char)str2[i] << " "; } cout << endl; #endif return 0; }View Code
发现直接copy和move并没有什么区别。
实际上,move的最大用法应该是用于移动构造函数。
对于某些带了指针类型的类,在复制其对象时,一般要使用深拷贝,参考string。
string内置一个char *指针,当你拷贝了当前string对象时,你不应该只复制指针,而应该要新开一块内存,把原指针指向的内存复制过去。
但是有一种情况例外,如果我可以保证原对象不用了的话,那我就可以执行浅拷贝,直接把指针的值复制过去,这样可以省去复制指针指向内存的开销。
以string为例子,深拷贝是复制char *指向的内存,而浅拷贝直接复制指针。
move的作用只是把参数变成右值,然后触发移动构造函数,或者移动赋值函数,仅此而已。
内容总结
以上是互联网集市为您收集整理的c++ 右值以及move全部内容,希望文章能够帮你解决c++ 右值以及move所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。