forked from YunWGui/cpp_study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontainer.cpp
More file actions
208 lines (166 loc) · 3.06 KB
/
container.cpp
File metadata and controls
208 lines (166 loc) · 3.06 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright (c) 2020 by Chrono
//
// g++ container.cpp -std=c++11 -o a.out;./a.out
// g++ container.cpp -std=c++14 -o a.out;./a.out
// g++ container.cpp -std=c++14 -I../common -o a.out;./a.out
#include <cassert>
#include <iostream>
#include <complex>
#include <array>
#include <vector>
#include <deque>
#include <list>
#include <forward_list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
using namespace std;
class Point final
{
//private:
public:
int x = 0;
public:
Point(int a) noexcept : x(a)
{
cout << "ctor" << x << endl;
}
Point() noexcept
{
cout << "ctor" << x << endl;
}
~Point() noexcept
{
cout << "dtor" << x << endl;
}
Point(const Point& p) noexcept
{
x = p.x;
cout << "copy ctor"<< x << endl;
}
Point(Point&& p) noexcept
{
x = std::move(p.x);
cout << "move ctor" << x << endl;
}
};
void case1()
{
vector<Point> v;
v.reserve(10);
Point p;
v.push_back(p);
v.push_back(std::move(p));
v.emplace_back(1);
}
void case2()
{
array<int, 2> arr;
assert(arr.size() == 2);
vector<int> v(2);
for(int i = 0; i < 10; i++) {
v.emplace_back(i);
}
//cout << v.size() << endl;
assert(v.size() == 12);
deque<int> d;
d.emplace_back(9);
d.emplace_front(1);
assert(d.size() == 2);
}
bool operator<(const Point& a, const Point& b)
{
return a.x < b.x;
}
void case3()
{
set<Point> s;
s.emplace(7);
s.emplace(3);
for(auto& p : s) {
cout << p.x << ",";
}
cout << endl;
}
#if 0
using complex_t = complex<double>;
// add < compare to complex
namespace std{
bool operator<(const complex_t& a, const complex_t& b)
{
return a.real() < b.real();
}
}
void case3()
{
set<complex_t> s;
s.emplace(3, 1);
s.emplace(1, 1);
for(auto& c : s) {
cout << c << ",";
}
cout << endl;
}
#endif
void case4()
{
set<int> s = {7, 3, 9};
for(auto& x : s) {
cout << x << ",";
}
cout << endl;
auto comp = [](auto a, auto b)
{
return a > b;
};
set<int, decltype(comp)> gs(comp);
std::copy(begin(s), end(s),
inserter(gs, gs.end()));
for(auto& x : gs) {
cout << x << ",";
}
cout << endl;
}
void case5()
{
using map_type =
unordered_map<int, string>;
map_type dict;
dict[1] = "one";
dict.emplace(2, "two");
dict[10] = "ten";
for(auto& x : dict) {
cout << x.first << "=>"
<< x.second << ",";
}
cout << endl;
}
bool operator==(const Point& a, const Point& b)
{
return a.x == b.x;
}
void case6()
{
auto hasher = [](const auto& p)
{
return std::hash<int>()(p.x);
};
unordered_set<Point, decltype(hasher)> s(10, hasher);
s.emplace(7);
s.emplace(3);
for(auto& p : s) {
cout << p.x << ",";
}
cout << endl;
}
int main()
{
case1();
case2();
case3();
case4();
case5();
case6();
cout << "containter demo" << endl;
}