-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmove.cpp
More file actions
29 lines (23 loc) · 744 Bytes
/
move.cpp
File metadata and controls
29 lines (23 loc) · 744 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <utility>
#include <vector>
#include <string>
using namespace std;
int main()
{
auto str = "Hello"s;
vector<string> v;
// uses the push_back(const T&) overload, which means
// we'll incur the cost of copying str
v.push_back(str);
cout << "After copy, str is \"" << str << "\"\n";
// uses the rvalue reference push_back(T&&) overload,
// which means no strings will copied; instead, the contents
// of str will be moved into the vector. This is less
// expensive, but also means str might now be empty.
v.push_back(move(str));
cout << "After move, str is \"" << str << "\"\n";
cout << "The contents of the vector are \"" << v[0]
<< "\", \"" << v[1] << "\"\n";
return 0;
}