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

C++中std::move()的正确使用相关例子

一、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;    // hello
    std::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()

相关文章:

  • C语言个人笔记
  • 前端面试总结3
  • Lumion 与 Enscape 怎么选?附川翔云电脑适配指南
  • 在3ds Max中视口显示为黑色或深灰色
  • leetcode68.左右文本对齐
  • 《DeepSeek RAG 增强检索知识库系统》Ollama RAG 知识库上传、解析和验证之四
  • mysql 禁止 读 某个 表
  • 第18章:基于Global Context Vision Transformers(GCTx_unet)网络实现的oct图像中的黄斑水肿和裂孔分割
  • 【Audio开发三】音频audio中帧frameSize ,周期大小periodsize,缓冲区buffer原理详解以及代码流程分析
  • 《嵌入式开发实战:基于Linux串口的LED屏显系统设计与实现》
  • Clickhouse试用单机版部署
  • 【完整可用】使用openhtmltopdf生成PDF(带SVG)
  • 策略模式结合模板方法模式
  • Go语言入门-反射4(动态构建类型)
  • PyCharm Community社区版链接WSL虚拟环境
  • 【笔记ing】AI大模型-01大模型基础综述
  • 医学科研工作者的AI助手:高效生成文献结构图和实验流程图
  • U9新开发webapi无授权
  • 使用Docker创建postgres
  • 智慧医院室内导航系统架构拆解:技术选型与性能攻坚指南
  • 网站友链查询/直通车优化推广
  • 网站建设费用价格/手机网站自助建站系统
  • 怎么做盗版网站赚钱/简述网站建设流程
  • 哪里的郑州网站建设/可以直接打开网站的网页
  • 微信网站开发文档/自助建站免费搭建个人网站
  • 凡科的模板做网站/谷歌google下载