-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector_move_insert.cc
More file actions
107 lines (99 loc) · 2.99 KB
/
vector_move_insert.cc
File metadata and controls
107 lines (99 loc) · 2.99 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <vector>
#include <iostream>
class Test
{
public:
Test(){
std::cout << "default construct\n";
data_ = new int(0);
}
Test(int a){
std::cout << "construct " << a << " \n";
data_ = new int(a);
}
Test(const Test &rhs){
std::cout << "copy construct " << *rhs.data_ << " \n";
data_ = new int();
*data_ = *rhs.data_;
}
Test& operator=(const Test &rhs){
std::cout << "copy operator " << *rhs.data_ << " \n";
data_ = new int();
*data_ = *rhs.data_;
return *this;
}
Test(Test &&rhs){
std::cout << "move construct " << *rhs.data_ << " \n";
data_ = rhs.data_;
rhs.data_ = nullptr;
}
Test& operator=(Test &&rhs){
std::cout << "move operator " << *rhs.data_ << " \n";
data_ = rhs.data_;
rhs.data_ = nullptr;
return *this;
}
~Test(){
delete data_;
data_ = nullptr;
}
int data(){
if(data_)
return *data_;
else{
return 0;
}
}
private:
int *data_;
};
int main()
{
// std::vector<int> new_vec {1, 2};
// auto print_v = [&new_vec](std::vector<int> &tail_vec){
// std::cout << "the lhs one is: ";
// for(auto &&ele : new_vec){
// std::cout << ele << " ";
// }
// std::cout << "after append, the rhs one is: ";
// for(auto &&ele : tail_vec){
// std::cout << ele << " ";
// }
// std::cout << std::endl;
// };
// std::vector<int> copy_vec {3, 4};
// new_vec.insert(new_vec.end(), copy_vec.begin(), copy_vec.end());
// print_v(copy_vec);
// std::vector<int> move_vec {33, 44};
// new_vec.reserve(7);
// std::cout << "reserve finished\n";
// new_vec.insert(new_vec.end(), std::make_move_iterator(move_vec.begin()), std::make_move_iterator(move_vec.end()));
// print_v(move_vec);
/*
- 只能使vector的每个元素在insert时发生拷贝。
- swap的作用是交换两个容器的内容,不针对单个元素执行move、copy、swap操作。
- 无法将整个vector的内容移动到另一个vector后面。
*/
std::vector<Test> new_vec {1};
new_vec.emplace_back(2);
auto print_v = [&new_vec](std::vector<Test> &tail_vec){
std::cout << "the lhs one is: ";
for(auto &&ele : new_vec){
std::cout << ele.data() << " ";
}
std::cout << "after append, the rhs one is: ";
for(auto &&ele : tail_vec){
std::cout << ele.data() << " ";
}
std::cout << std::endl;
};
std::vector<Test> copy_vec {3, 4};
new_vec.insert(new_vec.end(), copy_vec.begin(), copy_vec.end());
print_v(copy_vec);
std::vector<Test> move_vec {33, 44};
new_vec.reserve(7);
std::cout << "reserve finished\n";
new_vec.insert(new_vec.end(), std::make_move_iterator(move_vec.begin()), std::make_move_iterator(move_vec.end()));
print_v(move_vec);
return 0;
}