您的位置:首页 > 教育 > 锐评 > 成都网站建设专业乐云seo_神马收录提交入口_网络营销公司名字大全_深圳百度关键词排名

成都网站建设专业乐云seo_神马收录提交入口_网络营销公司名字大全_深圳百度关键词排名

2025/11/4 21:07:18 来源:https://blog.csdn.net/qq_36812406/article/details/147059667  浏览:    关键词:成都网站建设专业乐云seo_神马收录提交入口_网络营销公司名字大全_深圳百度关键词排名
成都网站建设专业乐云seo_神马收录提交入口_网络营销公司名字大全_深圳百度关键词排名

一、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()

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com