MCPcopy Create free account
hub / github.com/changkun/modern-cpp-tutorial / main

Function main

code/3/3.5.cpp:14–32  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

12#include <string> // std::string
13
14int main() {
15
16 std::string str = "Hello world.";
17 std::vector<std::string> v;
18
19 // 将使用 push_back(const T&), 即产生拷贝行为
20 v.push_back(str);
21 // 将输出 "str: Hello world."
22 std::cout << "str: " << str << std::endl;
23
24 // 将使用 push_back(const T&&), 不会出现拷贝行为
25 // 而整个字符串会被移动到 vector 中,所以有时候 std::move 会用来减少拷贝出现的开销
26 // 这步操作后, str 中的值会变为空
27 v.push_back(std::move(str));
28 // 将输出 "str: "
29 std::cout << "str: " << str << std::endl;
30
31 return 0;
32}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected