06-vector.cpp
663 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <memory>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Example
{
public:
Example()
{
cout << "Example::Example()" << endl;
}
Example(int a)
{
cout << "Example::Example(" << a << ")" << endl;
}
~Example()
{
cout << "Example::~Example()" << endl;
}
};
int main()
{
vector<unique_ptr<Example>> v(10);
cout << v.size() << endl;
v.push_back(make_unique<Example>(1));
v.push_back(unique_ptr<Example>(new Example()));
cout << v.size() << endl;
vector<unique_ptr<Example>> w = move(v);
cout << v.size() << endl;
cout << w.size() << endl;
unique_ptr<Example> a(move(w[10]));
}