C++复习swap and move
来源:互联网 发布:homer软件收费吗 编辑:程序博客网 时间:2024/06/08 07:18
Swapping doubles is easily implemented with three copies, However, although the same logic works to swap larger types, it comes with a significant cost: Now the copies are very expensive. In C++11, if the right-hand side of the assignment operator(or constructor) is an rvalue, then if the object supports moving, we can automatically avoid copies.
function std::move exists that converts any lvalue(or rvalue)into an rvalue. Note that the name is misleading; std::move doesn't move anything; rather, it makes a value subject to be moved.
void swap(double &x,double &y){ double tmp=x; x=y; y=tmp;}//replace expensive copies with moves;void swap(vector<string> &x,vector<string> &y){ vector<string>tmp=static_cast<vector<string>&&>(x); x=static_cast<vector<string>&&>(y); y=static_cast<vector<string>&&>(tmp);}//The syntax of a static cast is daunting;void swap(vector<string>&x,vector<string>&y){ vector<string>tmp=std::move(x); x=std::move(y); y=std::move(tmp);}//move function.
我自己写的小网站www.caozhicong.com
阅读全文
0 0
- C++复习swap and move
- C++黑魔法系列2: lvalue, move constructor, copy and swap
- Move.c && Move.h
- Move.h && Move.c
- STL 之 utility pair swap move
- linux move and install
- move and forward
- C swap
- DICOM医学图像处理:fo-dicom网络传输之C-FIND and C-MOVE
- copy and swap
- Copy-and-swap
- copy and swap
- Compare and Swap(CAS)
- Copy-and-swap
- [agc019e]Shuffle and Swap
- Send and Move to SDCard
- C# Control Drag and Move
- MongoDB move chunk and balancing
- java导出excel 轻松导出百万条记录
- 联想 k50 t5 手机开启 usb 调试模式
- 运维的耻辱感
- 工程师联盟 V0.1
- 为什么说Python会成为最流行的编程语言?
- C++复习swap and move
- Java 拾遗补阙 ----- Switch case语句
- 腾讯云下安装docker
- Docker学习笔记(一)
- Docker学习笔记(二)
- 如何用sed命令往空文件中写入数据
- MIT高级算法之并行计算
- Docker学习笔记(三)之容器的基本操作
- Docker学习笔记(四)之容器查看启动终止删除