Skip to content

std::move减少数据拷贝 #2

Description

@dinghaodhd

std::move是将对象的状态或者所有权从一个对象转移到另一个对象,只是转移,没有内存的搬迁或者内存拷贝。
std::string str = "Hello";
std::vectorstd::string v;
//调用常规的拷贝构造函数,新建字符数组,拷贝数据
v.push_back(str);
std::cout << "After copy, str is "" << str << ""\n";
//调用移动构造函数,掏空str,掏空后,最好不要使用str
v.push_back(std::move(str));

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions