-
Notifications
You must be signed in to change notification settings - Fork 0
/
05-operate.ixx
201 lines (170 loc) · 2.81 KB
/
05-operate.ixx
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
#include <string>
#include <complex>
#include <complex>
#include <thread>
#include <cassert>
import _03_module;
export module _05_operate;
using namespace std;
class X {
public:
X(int);
X();
X(const X&);
X(X&&);
X& operator=(const X&);
X& operator=(X&&);
~X();
//...
};
//移动 / 拷贝:
// a. 赋值
// b. 对象初始值
// c. 参数实参
// d. 函数返回值
// e. 作为异常
class Y {
public:
Y(int);
Y(const Y&) = default;
Y(Y&) = default;
Y(Y&&) = default;
};
// the rule of zero: 要么定义所有;要么1个也不定义;
struct Z {
Vector v;
string s;
};
void f() {
Z z1(10);
Z z2 = z1;
}
class Shape {
public:
Shape(const Shape&) = delete; // 无拷贝函数
};
// 5.1.2 类型转换
complex z1 = 3;
complex z2 = z1 * 1;
Vector v1 = 7;
class V {
public:
explicit V(int s); // 禁止 int -> vector的隐式转换
};
// 5.1.3 成员初始值
class cmplx {
double re = 0;
double im = 0;
public:
cmplx(double r, double i): re{r}, im{i}{}
cmplx(double r): re{r} {}
cmplx(){}
};
// 5.2 拷贝&移动
void bad_copy(Vector v1) {
Vector v2 = v1;
v1[0] = 2;
v2[1] = 3;
}
class _V {
public:
_V();
_V(int s);
~_V() { delete[] elem; }
_V(const _V& a); // 拷贝构造函数
_V& operator=(const _V& a); // 拷贝赋值运算符
double& operator[](int i) { return elem[i]; }
_V operator+(_V& a) {
_V res(sz);
for (int i = 0; i != a.sz; i++) {
res[i] = elem[i] + a[i];
}
return res;
}
_V(_V&& a) {// 移动构造函数
elem = a.elem;
sz = a.sz;
a.elem = nullptr;
a.sz = 0;
};
_V& operator=(_V&& a){// 移动赋值运算符
elem = a.elem;
sz = a.sz;
a.elem = nullptr;
a.sz = 0;
return *this;
}
double* elem;
int sz;
};
_V::_V(const _V& a){
elem = new double[a.sz];
sz = a.sz;
for (int i = 0; i != sz; i++) {
elem[i] = a.elem[i];
}
}
_V& _V::operator=(const _V& a) {
double* p = new double[a.sz];
for (int i = 0; i != a.sz; i++) {
p[i] = a.elem[i];
}
delete[] elem;
elem = p;
sz = a.sz;
return *this;
}
// 5.2.2移动容器
_V f(_V& x, _V& y, _V& z) {
_V r;
r = x + y + z;
_V a(100);
_V b(200);
_V c(200);
c = a; // 拷贝
b = std::move(a); // 移动赋值
return c; // 移动
}
// 5.3 资源管理
vector<thread> my_threads;
Vector init(int n) {
thread t;
my_threads.push_back(move(t));
Vector vec(n);
for (int i = 0; i != vec.size(); i++) {
vec[i] = 777;
}
return vec;
}
auto v = init(1'000'000);
// 5.4 常规操作
// a. ==,!=, <, <=, >, >=
// b. size(), begin(), end(),
// c. >>, <<
// d. udl
// e. swap()
// f. hash<>
// 5.4.1 比较
void _5_4_1() {
int i = 0;
int j = i;
assert(i==j);
}
// 5.4.2 容器操作
void _5_4_2(vector<int> c) {
for (size_t i = 0; i < c.size(); i++) {
c[i] = 0;
}
for (auto p = c.begin(); p != c.end(); p++) {
*p = 0;
}
for (auto& x : c) {
x = 0;
}
}
// 5.4.3 io
// 5.4.4 用户自定义字面值
complex<double> operator""_i(long double arg) {
return complex<double>(0, arg);
}
complex<long double> z = 1.1 + 5.0_i;