| 12 | #include <string> // std::string |
| 13 | |
| 14 | int 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 | } |
nothing calls this directly
no outgoing calls
no test coverage detected