mirror of
https://github.com/changkun/modern-cpp-tutorial.git
synced 2025-12-17 12:44:40 +03:00
prepare for c++17
This commit is contained in:
32
code/3/3.5.cpp
Normal file
32
code/3/3.5.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
//
|
||||
// 3.5.cpp
|
||||
// c++1x tutorial
|
||||
//
|
||||
// created by changkun at shiyanlou.com
|
||||
//
|
||||
// 移动语义
|
||||
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user