-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_bonus.cpp
491 lines (344 loc) · 11.3 KB
/
main_bonus.cpp
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main_bonus.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: athirion <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/27 14:07:17 by athirion #+# #+# */
/* Updated: 2023/02/27 17:36:18 by athirion ### ########.fr */
/* */
/* ************************************************************************** */
# include "includes/set.hpp"
# include <set>
#include <iostream>
using namespace NAMESPACE;
void set_constructor(void) {
std::cout << ">>> SET CONSTRUCTORS <<<\n" << std::endl;
// (1) Default constructor
set<std::string> a;
a.insert("cat");
a.insert("dog");
a.insert("horse");
set<std::string>::iterator it1 = a.begin();
set<std::string>::iterator ite1 = a.end();
for(; it1 != ite1; it1++)
std::cout << *it1 << ' ';
std::cout << '\n';
// (2) Iterator constructor
set<std::string> b(a.find("dog"), a.end());
set<std::string>::iterator it2 = b.begin();
set<std::string>::iterator ite2 = b.end();
for(; it2 != ite2; it2++)
std::cout << *it2 << ' ';
std::cout << '\n';
// (3) Copy constructor
set<std::string> c(a);
c.insert("another horse");
set<std::string>::iterator it3 = c.begin();
set<std::string>::iterator ite3 = c.end();
for(; it3 != ite3; it3++)
std::cout << *it3 << ' ';
std::cout << '\n';
std::cout << std::endl;
}
void set_assignment_op(void) {
std::cout << ">>> SET ASSIGNMENT OPERATOR <<<\n" << std::endl;
int myints[]={ 12,82,37,64,15 };
set<int> first (myints,myints+5); // set with 5 ints
set<int> second; // empty set
second = first; // now second contains the 5 ints
first = set<int>(); // and first is empty
std::cout << "Size of first: " << int (first.size()) << '\n';
std::cout << "Size of second: " << int (second.size()) << '\n';
std::cout << std::endl;
}
void set_begin_end(void) {
std::cout << ">>> SET BEGIN END <<<\n" << std::endl;
int myints[] = {75,23,65,42,13};
set<int> myset (myints,myints+5);
std::cout << "myset contains:";
for (set<int>::iterator it=myset.begin(); it!=myset.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
std::cout << std::endl;
}
void set_rbegin_rend(void) {
std::cout << ">>> SET RBEGIN REND <<<\n" << std::endl;
int myints[] = {21,64,17,78,49};
set<int> myset (myints,myints+5);
set<int>::reverse_iterator rit;
std::cout << "myset contains:";
for (rit=myset.rbegin(); rit != myset.rend(); ++rit)
std::cout << ' ' << *rit;
std::cout << '\n';
std::cout << std::endl;
}
void set_empty(void) {
std::cout << ">>> SET EMPTY <<<\n" << std::endl;
set<int> myset;
myset.insert(20);
myset.insert(30);
myset.insert(10);
std::cout << "myset contains:";
while (!myset.empty())
{
std::cout << ' ' << *myset.begin();
myset.erase(myset.begin());
}
std::cout << '\n';
std::cout << std::endl;
}
void set_size(void) {
std::cout << ">>> SET SIZE <<<\n" << std::endl;
set<int> myints;
std::cout << "0. size: " << myints.size() << '\n';
for (int i=0; i<10; ++i) myints.insert(i);
std::cout << "1. size: " << myints.size() << '\n';
myints.insert (100);
std::cout << "2. size: " << myints.size() << '\n';
myints.erase(5);
std::cout << "3. size: " << myints.size() << '\n';
std::cout << std::endl;
}
void set_max_size(void) {
std::cout << ">>> SET MAX_SIZE <<<\n" << std::endl;
int i;
set<int> myset;
if (myset.max_size()>1000)
{
for (i=0; i<1000; i++) myset.insert(i);
std::cout << "The set contains 1000 elements.\n";
}
else std::cout << "The set could not hold 1000 elements.\n";
std::cout << std::endl;
}
void set_insert(void) {
std::cout << ">>> SET INSERT <<<\n" << std::endl;
set<int> myset;
set<int>::iterator it;
pair<set<int>::iterator,bool> ret;
// set some initial values:
for (int i=1; i<=5; ++i) myset.insert(i*10); // set: 10 20 30 40 50
ret = myset.insert(20); // no new element inserted
if (ret.second==false) it=ret.first; // "it" now points to element 20
myset.insert (it,25); // max efficiency inserting
myset.insert (it,24); // max efficiency inserting
myset.insert (it,26); // no max efficiency inserting
int myints[]= {5,10,15}; // 10 already in set, not inserted
myset.insert (myints,myints+3);
std::cout << "myset contains:";
for (it=myset.begin(); it!=myset.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
std::cout << std::endl;
}
void set_erase(void) {
std::cout << ">>> SET ERASE <<<\n" << std::endl;
set<int> myset;
set<int>::iterator it;
// insert some values:
for (int i=1; i<10; i++) myset.insert(i*10); // 10 20 30 40 50 60 70 80 90
it = myset.begin();
++it; // "it" points now to 20
myset.erase (it);
myset.erase (40);
it = myset.find (60);
myset.erase (it, myset.end());
std::cout << "myset contains:";
for (it=myset.begin(); it!=myset.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
std::cout << std::endl;
}
void set_swap(void) {
std::cout << ">>> SET SWAP <<<\n" << std::endl;
int myints[]={12,75,10,32,20,25};
set<int> first (myints,myints+3); // 10,12,75
set<int> second (myints+3,myints+6); // 20,25,32
first.swap(second);
std::cout << "first contains:";
for (set<int>::iterator it=first.begin(); it!=first.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
std::cout << "second contains:";
for (set<int>::iterator it=second.begin(); it!=second.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
std::cout << std::endl;
}
void set_clear(void) {
std::cout << ">>> SET CLEAR <<<\n" << std::endl;
set<int> myset;
myset.insert (100);
myset.insert (200);
myset.insert (300);
std::cout << "myset contains:";
for (set<int>::iterator it=myset.begin(); it!=myset.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
myset.clear();
myset.insert (1101);
myset.insert (2202);
std::cout << "myset contains:";
for (set<int>::iterator it=myset.begin(); it!=myset.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
std::cout << std::endl;
}
void set_key_comp(void) {
std::cout << ">>> SET KEY COMP <<<\n" << std::endl;
set<int> myset;
int highest;
set<int>::key_compare mycomp = myset.key_comp();
for (int i=0; i<=5; i++) myset.insert(i);
std::cout << "myset contains:";
highest=*myset.rbegin();
set<int>::iterator it=myset.begin();
do {
std::cout << ' ' << *it;
} while ( mycomp(*(++it),highest) );
std::cout << '\n';
std::cout << std::endl;
}
void set_value_comp(void) {
std::cout << ">>> SET VALUE COMP <<<\n" << std::endl;
set<int> myset;
set<int>::value_compare mycomp = myset.value_comp();
for (int i=0; i<=5; i++) myset.insert(i);
std::cout << "myset contains:";
int highest=*myset.rbegin();
set<int>::iterator it=myset.begin();
do {
std::cout << ' ' << *it;
} while ( mycomp(*(++it),highest) );
std::cout << '\n';
std::cout << std::endl;
}
void set_find(void) {
std::cout << ">>> SET FIND <<<\n" << std::endl;
set<int> myset;
set<int>::iterator it;
// set some initial values:
for (int i=1; i<=5; i++) myset.insert(i*10); // set: 10 20 30 40 50
it=myset.find(20);
myset.erase (it);
myset.erase (myset.find(40));
std::cout << "myset contains:";
for (it=myset.begin(); it!=myset.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
std::cout << std::endl;
}
void set_count(void) {
std::cout << ">>> SET COUNT <<<\n" << std::endl;
set<int> myset;
// set some initial values:
for (int i=1; i<5; ++i) myset.insert(i*3); // set: 3 6 9 12
for (int i=0; i<10; ++i)
{
std::cout << i;
if (myset.count(i)!=0)
std::cout << " is an element of myset.\n";
else
std::cout << " is not an element of myset.\n";
}
std::cout << std::endl;
}
void set_lower_upper_bound(void) {
std::cout << ">>> SET LOWERBOUND UPPERBOUND <<<\n" << std::endl;
set<int> myset;
set<int>::iterator itlow,itup;
for (int i=1; i<10; i++) myset.insert(i*10); // 10 20 30 40 50 60 70 80 90
itlow=myset.lower_bound (30); // ^
itup=myset.upper_bound (60); // ^
myset.erase(itlow,itup); // 10 20 70 80 90
std::cout << "myset contains:";
for (set<int>::iterator it=myset.begin(); it!=myset.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
std::cout << std::endl;
}
void set_equal_range(void) {
std::cout << ">>> SET LOWERBOUND UPPERBOUND <<<\n" << std::endl;
set<int> myset;
for (int i=1; i<=5; i++) myset.insert(i*10); // myset: 10 20 30 40 50
pair<set<int>::const_iterator,set<int>::const_iterator> ret;
ret = myset.equal_range(30);
std::cout << "the lower bound points to: " << *ret.first << '\n';
std::cout << "the upper bound points to: " << *ret.second << '\n';
std::cout << std::endl;
}
void set_get_allocator(void) {
std::cout << ">>> SET GET ALLOCATOR <<<\n" << std::endl;
set<int> myset;
int * p;
unsigned int i;
// allocate an array of 5 elements using myset's allocator:
p=myset.get_allocator().allocate(5);
// assign some values to array
for (i=0; i<5; i++) p[i]=(i+1)*10;
std::cout << "The allocated array contains:";
for (i=0; i<5; i++) std::cout << ' ' << p[i];
std::cout << '\n';
myset.get_allocator().deallocate(p,5);
std::cout << std::endl;
}
void set_relational_op(void) {
std::cout << ">>> SET RELATIONAL OPERATORS <<<\n" << std::endl;
set<int> alice;
alice.insert(1);
alice.insert(2);
alice.insert(3);
set<int> bob;
bob.insert(7);
bob.insert(8);
bob.insert(9);
bob.insert(10);
set<int> eve;
eve.insert(1);
eve.insert(2);
eve.insert(3);
std::cout << std::boolalpha;
// Compare non equal containers
std::cout << "alice == bob returns " << (alice == bob) << '\n';
std::cout << "alice != bob returns " << (alice != bob) << '\n';
std::cout << "alice < bob returns " << (alice < bob) << '\n';
std::cout << "alice <= bob returns " << (alice <= bob) << '\n';
std::cout << "alice > bob returns " << (alice > bob) << '\n';
std::cout << "alice >= bob returns " << (alice >= bob) << '\n';
std::cout << '\n';
// Compare equal containers
std::cout << "alice == eve returns " << (alice == eve) << '\n';
std::cout << "alice != eve returns " << (alice != eve) << '\n';
std::cout << "alice < eve returns " << (alice < eve) << '\n';
std::cout << "alice <= eve returns " << (alice <= eve) << '\n';
std::cout << "alice > eve returns " << (alice > eve) << '\n';
std::cout << "alice >= eve returns " << (alice >= eve) << '\n';
std::cout << std::endl;
}
int main()
{
/*
** SET
*/
set_constructor();
set_assignment_op();
set_begin_end();
set_rbegin_rend();
set_empty();
set_size();
set_max_size();
set_insert();
set_erase();
set_swap();
set_clear();
set_key_comp();
set_value_comp();
set_find();
set_count();
set_lower_upper_bound();
set_equal_range();
set_get_allocator();
set_relational_op();
}