FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
examples/cpp/03.cpp at main · makelinux/examples · GitHub
makelinux
/
examples
Public
Notifications
You must be signed in to change notification settings
Fork
51
Star
140
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
examples
/
cpp
/
03.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
312 lines (257 loc) · 5.93 KB
Breadcrumbs
examples
/
cpp
/
03.cpp
Copy path
File metadata and controls
312 lines (257 loc) · 5.93 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
*
@file
@brief C++03 / C++98
@defgroup CPP03 C++03 examples
@brief C++03 / C++98
https://en.wikipedia.org/wiki/C++03
@{
*/
#
include
<
bits/stdc++.h
>
using
namespace
std
;
/*
*
@defgroup lang03 Language
@{
*/
void
init_03
()
{
//
https://en.cppreference.com/w/cpp/language/copy_initialization
int
x3 = {
3
};
double
x4 = {
3.0
};
struct
point
{
int
x, y;
};
point p1 = {
1
,
2
};
(
void
)p1.
x
;
(
void
)p1.
y
;
#
if
gcc_extension
//
designated initializers
//
https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html
__extension__ point gpp_ext = { .
x
=
1
};
//
C99-like gcc extension
__extension__ point gcc_ext = { x :
1
};
//
C-like gcc extension
#
endif
//
Mutable
struct
struct_with_mutable
{
struct_with_mutable
() {};
mutable
int
m;
}
const
const_struct_with_mutable;
const_struct_with_mutable.
m
=
2
;
assert
(const_struct_with_mutable.
m
==
2
);
int
y =
0
;
int
& reference = y;
reference =
1
;
assert
(y ==
1
);
}
/*
*
[set](https://en.cppreference.com/w/cpp/container/set)
[map](https://en.cppreference.com/w/cpp/container/map)
[multimap](https://en.cppreference.com/w/cpp/container/multimap)
*/
void
associative_containers_03
()
{
set<
int
> s;
s.
insert
(
1
);
s.
insert
(
2
);
assert
(*s.
find
(
1
) ==
1
);
assert
(s.
find
(
3
) == s.
end
());
map<
char
,
int
> m;
m[
'
a
'
] =
1
;
m.
insert
(
make_pair
(
'
b
'
,
2
));
++m[
'
a
'
];
assert
(m[
'
a
'
] ==
2
);
multimap<
char
,
int
> mm;
mm.
insert
(
make_pair
(
'
c
'
,
1
));
mm.
insert
(
make_pair
(
'
b
'
,
2
));
mm.
insert
(
make_pair
(
'
a
'
,
3
));
mm.
insert
(
make_pair
(
'
a
'
,
4
));
multimap<
char
,
int
>::iterator i = mm.
find
(
'
a
'
);
assert
(i->
second
==
3
);
i++;
assert
(i->
second
==
4
);
i++;
assert
(i->
first
==
'
b
'
);
assert
(i->
second
==
2
);
}
template
<
class
C
>
void
test_generic_container
(C& c)
{
assert
(c.
empty
());
assert
(c.
max_size
() >
1000
);
c.
push_back
(
0
);
assert
(c.
front
() ==
0
);
assert
(c.
back
() ==
0
);
assert
(c.
size
() ==
1
);
c.
push_back
(
1
);
c.
push_back
(
2
);
c.
push_back
(
3
);
assert
(c.
size
() ==
4
);
for
(
typename
C::iterator i = c.
begin
(); i != c.
end
();) {
if
(*i ==
1
) {
i = c.
erase
(i);
continue
;
}
if
(*i ==
2
) {
c.
erase
(i++);
continue
;
}
++i;
}
assert
(c.
size
() ==
2
);
assert
(c.
front
() ==
0
);
assert
(c.
back
() ==
3
);
c.
assign
(
4
,
1
);
assert
(c.
size
() ==
4
);
c.
clear
();
}
template
<
class
V
>
void
test_vector_container
(V& v)
{
string err;
try
{
v.
at
(
666
) =
0
;
}
catch
(out_of_range
const
& exc) {
err = exc.
what
();
}
assert
(err.
length
());
assert
(v[
0
] ==
1
);
v.
resize
(
4
);
int
arr[] = {
1
,
2
,
3
};
v.
insert
(v.
begin
(), arr, arr +
3
);
assert
(v[
1
] ==
2
);
}
//
/ [container](https://en.cppreference.com/w/cpp/container)
void
container_03
()
{
list<
int
> l;
test_generic_container
(l);
vector<
int
> v;
test_generic_container
(v);
test_vector_container
(v);
v.
reserve
(
10
);
assert
(v.
capacity
() ==
10
);
deque<
int
> d;
test_generic_container
(d);
test_vector_container
(d);
stack<
int
> s;
s.
push
(
1
);
assert
(s.
top
() ==
1
);
s.
push
(
2
);
assert
(s.
top
() ==
2
);
s.
pop
();
assert
(s.
top
() ==
1
);
s.
pop
();
assert
(s.
empty
());
queue<
int
> q;
q.
push
(
1
);
q.
push
(
2
);
assert
(q.
front
() ==
1
);
assert
(q.
back
() ==
2
);
q.
pop
();
assert
(q.
front
() ==
2
);
q.
pop
();
assert
(q.
empty
());
priority_queue<
int
> pq;
pq.
push
(
2
);
pq.
push
(
3
);
pq.
push
(
1
);
assert
(pq.
top
() ==
3
);
associative_containers_03
();
}
/*
*
@fn void sort_03()
http://www.cplusplus.com/reference/algorithm/sort/?kw=sort
Compare with @ref lambda::sort_11 and @ref sort_libc
*/
//
/ @private
bool
func
(
int
i,
int
j) {
return
i < j; }
//
/ @private
struct
_
{
bool
operator
()(
int
i,
int
j) {
return
i < j; }
} functor;
void
sort_03
()
{
int
a[] = {
5
,
7
,
4
,
2
,
8
,
6
,
1
,
9
,
0
,
3
};
list<
int
>
list
(a, a +
sizeof
a /
sizeof
a[
0
]);
list.
sort
();
vector<
int
>
v
(a, a +
sizeof
a /
sizeof
a[
0
]);
//
using default comparison (operator <):
sort
(v.
begin
(), v.
begin
() +
4
);
//
using function as comp
sort
(v.
begin
() +
4
, v.
end
(), func);
//
sort using a standard library compare function object
sort
(v.
begin
(), v.
end
(), greater<
int
>());
//
using object as comp
sort
(v.
begin
(), v.
end
(), functor);
int
prev = -
1
;
for
(vector<
int
>::iterator i = v.
begin
(); i != v.
end
(); ++i) {
assert
(*i >= prev);
prev = *i;
}
}
//
/ [algorithm](https://en.cppreference.com/w/cpp/algorithm)
void
algo_03
()
{
int
a[] = {
1
,
2
,
3
};
vector<
int
>
v
(a, a +
sizeof
a /
sizeof
a[
0
]);
vector<
int
>
r
(
sizeof
a /
sizeof
a[
0
]);
reverse_copy
(v.
begin
(), v.
end
(), r.
begin
());
assert
(r[
0
] > r[
1
]);
sort_03
();
}
//
/ [reference](https://en.cppreference.com/w/cpp/language/reference)
int
&
a_ref
(
int
& a) {
return
a; }
struct
Common
{
int
n;
Common
(
int
x)
: n(x)
{
}
};
//
/ [ref](https://en.cppreference.com/w/cpp/language/virtual)
struct
Virtual_A
:
virtual
Common {
Virtual_A
()
: Common(
1
)
{
}
};
struct
Virtual_B
:
virtual
Common {
Virtual_B
()
: Common(
2
)
{
}
};
struct
Diamond
: Virtual_A, Virtual_B {
Diamond
()
: Common(
3
)
, Virtual_A()
, Virtual_B()
{
}
};
void
types_03
()
{
int
a =
0
;
assert
(
typeid
(
int
) ==
typeid
(a));
assert
(
typeid
(
int
).
name
() ==
string
(
"
i
"
));
a_ref
(a) =
2
;
assert
(a ==
2
);
Diamond d;
assert
(d.
Virtual_A
::n ==
3
);
assert
(d.
Virtual_B
::n ==
3
);
}
//
/ @}
int
main
(
void
)
{
assert
(__cplusplus ==
199711
);
init_03
();
container_03
();
algo_03
();
types_03
();
assert
(
min
(
1
,
2
) ==
1
);
assert
(
max
(
1
,
2
) ==
2
);
pair<
int
,
char
>
p
(
1
,
'
a
'
);
assert
(p.
first
==
1
);
assert
(p.
second
==
'
a
'
);
p =
make_pair
(
'
b
'
,
2
);
assert
(p.
first
==
'
b
'
);
}
//
/ @}
Back
|
FazBrowse Home
|
New Git URL