当前位置: 首页 > wzjs >正文

网站推广结束语云南建设工程有限公司

网站推广结束语,云南建设工程有限公司,iis7 wordpress,seo常用的优化工具一、std::move() 是干什么的&#xff1f; std::move() 的作用是&#xff1a;把一个左值强制转换为右值引用&#xff0c;从而触发移动语义。 它定义在头文件 <utility> 中。 二、什么是“移动语义”&#xff1f; 在 C11 之前&#xff0c;所有对象的赋值、参数传递都是通…

一、std::move() 是干什么的?

std::move() 的作用是:把一个左值强制转换为右值引用,从而触发移动语义

它定义在头文件 <utility> 中。


二、什么是“移动语义”?

在 C++11 之前,所有对象的赋值、参数传递都是通过拷贝完成的。

拷贝意味着深拷贝数据,但有些情况下我们只想“借走”或“转移”资源 —— 比如堆内存、文件句柄等。

移动语义允许我们**“转移资源的所有权”**,不再执行昂贵的拷贝。


三、std::move() 的基本例子

示例1:触发移动构造函数

#include <iostream>
#include <string>
#include <utility>int main() {std::string a = "hello";std::string b = std::move(a); // 移动构造,a 的内容被“搬走”std::cout << "b = " << b << std::endl;    // hellostd::cout << "a = " << a << std::endl;    // 可能是空的,未定义状态return 0;
}

输出可能是:

b = hello
a =

示例2:移动到函数参数中

void process(std::string s) {std::cout << "Processing: " << s << std::endl;
}int main() {std::string name = "ChatGPT";process(name);               // 拷贝构造process(std::move(name));    // 移动构造
}

如果你想避免拷贝、提高性能,传递临时对象或明确使用 std::move() 是一种方式。


示例3:在自定义类中实现移动构造函数

#include <iostream>
#include <cstring>class MyString {char* data;
public:// 构造函数MyString(const char* str) {data = new char[strlen(str)+1];strcpy(data, str);std::cout << "Constructed\n";}// 移动构造函数MyString(MyString&& other) noexcept {data = other.data;other.data = nullptr;std::cout << "Moved\n";}// 析构函数~MyString() {delete[] data;}
};int main() {MyString a("AI");MyString b = std::move(a);  // 使用移动构造函数
}

输出:

Constructed
Moved

四、使用 std::move() 的注意事项

情况说明
✅ 适合使用移动资源所有权,例如传递大对象、容器中的临时变量
❌ 不建议const 对象使用 std::move(),它不能被移动(因为移动构造需要非常量引用)
⚠️ 风险move 过的变量仍然存在,但已处于空状态,不能再访问原资源
const std::string str = "hello";
std::string movedStr = std::move(str); // 实际是拷贝,不会触发移动构造

五、容器中的 std::move()

vector 中使用 std::move 提高性能:

#include <vector>
#include <string>
#include <iostream>int main() {std::vector<std::string> v;std::string s = "OpenAI";v.push_back(s);              // 拷贝v.push_back(std::move(s));   // 移动
}

第二个 push_back 会触发移动构造,避免拷贝。


✅ 六、总结

项目拷贝语义移动语义
用途复制资源转移资源所有权
性能较慢更快,避免深拷贝
函数调用f(obj)f(std::move(obj))
触发条件左值右值或 std::move()


文章转载自:

http://KepFcEGa.dmtbs.cn
http://jy3YkZpS.dmtbs.cn
http://OwVAIEWp.dmtbs.cn
http://PkUHE1eu.dmtbs.cn
http://F3NjIxcf.dmtbs.cn
http://VmxIV0eX.dmtbs.cn
http://boN6EkFb.dmtbs.cn
http://5NlnRkPO.dmtbs.cn
http://y1AgIerv.dmtbs.cn
http://9rz0ywc0.dmtbs.cn
http://XoBZDDUb.dmtbs.cn
http://tBi41lNC.dmtbs.cn
http://fiIgnsM6.dmtbs.cn
http://5xfR84kw.dmtbs.cn
http://YFaBlhGB.dmtbs.cn
http://2cMI8KBk.dmtbs.cn
http://3RjlGPDY.dmtbs.cn
http://nHBd8VDN.dmtbs.cn
http://lWj9ptvn.dmtbs.cn
http://04AkRgMh.dmtbs.cn
http://K5vBggf4.dmtbs.cn
http://lKZhbsPF.dmtbs.cn
http://1LUx3MVT.dmtbs.cn
http://0HpBYZyb.dmtbs.cn
http://6eTsOsgZ.dmtbs.cn
http://YFeFzYLb.dmtbs.cn
http://YX66r5aI.dmtbs.cn
http://BsGqqmCh.dmtbs.cn
http://h55HZrad.dmtbs.cn
http://5yLmu2gw.dmtbs.cn
http://www.dtcms.com/wzjs/668361.html

相关文章:

  • 效果图网站推荐大全面包砖软件开发项目经理的职责
  • 网站建设 仿站seo优化怎么做
  • 云空间网站怎么做湖北自适应网站建设报价
  • 建设网站可以先买域名吗企业软文怎么写
  • 自动建站网站源码wordpress最新版中午
  • dedecms做论坛网站珠海市建设局网站
  • 佛山做网站的公司哪家好百度推广怎么优化
  • 网站建设公司上海做网站公司哪家好未来商城网站建设
  • 网站seo优化运营宿迁房产网备案查询
  • 网站建设工厂wordpress 用户名
  • 资源网站搭建长沙网络推广哪家
  • 做网站通常又什么开发完成wordpress仿站主题
  • 电影网站加盟可以做么充值中心网站怎么做
  • wordpress 总站模板新媒体营销论文
  • 荆门网站开发公司电话郑州模板建站系统
  • 只用html5可以做网站吗教学方面网站建设
  • 网站开发答辩网站图片动态换名
  • 织梦网站维护简洁印象wordpress企业主题
  • 网站建设中 敬请期待福建住房与城乡建设厅网站
  • 网站开发有哪些服务网站推广技巧和方法
  • 做神马网站优化快速排名软件云南做企业网站多少钱
  • 公司网站企业文化怎么做男男做暧暧视频网站
  • 郑州设计师网站化妆品网站的建设目标
  • 校园网站建设与管理成都市住房和城乡建设局
  • 找人做的网站 没登录口自己想做个网站怎么做
  • 西宁专业做网站的window优化大师
  • 广 做网站蓝光电影下载做淘宝代码的网站
  • 网站制作app排行榜前十名dede英文网站
  • 网站首页flash制作门户网站推广方式
  • wp网站开发国家建设官方网站