mirror of
https://github.com/changkun/modern-cpp-tutorial.git
synced 2025-12-17 12:44:40 +03:00
33 lines
854 B
C++
33 lines
854 B
C++
//
|
|
// 3.5.cpp
|
|
// modern c++ tutorial
|
|
//
|
|
// created by changkun at changkun.de
|
|
//
|
|
// 移动语义
|
|
|
|
#include <iostream> // std::cout
|
|
#include <utility> // std::move
|
|
#include <vector> // std::vector
|
|
#include <string> // std::string
|
|
|
|
int main() {
|
|
|
|
std::string str = "Hello world.";
|
|
std::vector<std::string> v;
|
|
|
|
// 将使用 push_back(const T&), 即产生拷贝行为
|
|
v.push_back(str);
|
|
// 将输出 "str: Hello world."
|
|
std::cout << "str: " << str << std::endl;
|
|
|
|
// 将使用 push_back(const T&&), 不会出现拷贝行为
|
|
// 而整个字符串会被移动到 vector 中,所以有时候 std::move 会用来减少拷贝出现的开销
|
|
// 这步操作后, str 中的值会变为空
|
|
v.push_back(std::move(str));
|
|
// 将输出 "str: "
|
|
std::cout << "str: " << str << std::endl;
|
|
|
|
return 0;
|
|
}
|