-
Notifications
You must be signed in to change notification settings - Fork 0
/
std.cpp
741 lines (587 loc) · 21 KB
/
std.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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
#include <algorithm>
#include <iostream>
#include <ostream>
#include <stdexcept>
#include <string>
#include <deque>
#include <time.h>
#if 1 //CREATE A REAL STL EXAMPLE
#include <map>
#include <stack>
#include <vector>
#include <set>
namespace ft = std;
#else
#include "map.hpp"
#include "stack.hpp"
#include "vector.hpp"
#include "set.hpp"
#endif
template<typename T>
void print_vector(ft::vector<T>& v, size_t id = 0, const std::string& delimiter = " ")
{
std::cout << "vector" << id << ": " << "[ ";
for (typename ft::vector<T>::iterator it1 = v.begin(); it1 != v.end(); it1++)
std::cout << *it1 << delimiter;
std::cout << "]" << std::endl;
}
template<typename T, typename U>
void print_map(const ft::map<T, U>& m, std::string comment = "")
{
std::cout << comment;
for (typename ft::map<T, U>::const_iterator it = m.begin(); it != m.end(); it++)
std::cout << it->first << " = " << it->second << "; ";
std::cout << '\n';
}
template<typename T>
void print_stack(ft::stack<T> s, size_t id = 0, const std::string& delimiter = "\n")
{
size_t n_elems = s.size();
std::cout << "stack" << id << ":\n";
for (; !s.empty(); --n_elems)
{
std::cout << "[ " << s.top() << " ]" << delimiter;
s.pop();
}
}
template<typename T>
void print_set(const ft::set<T>& s, std::string comment = "")
{
std::cout << comment;
for (typename ft::set<T>::const_iterator it = s.begin(); it != s.end(); it++)
std::cout << *it << " -> ";
std::cout << "(end)" << std::endl;
}
#include <stdlib.h>
#define MAX_RAM 4294967296
#define BUFFER_SIZE 4096
struct Buffer
{
int idx;
char buff[BUFFER_SIZE];
};
#define COUNT (MAX_RAM / (int)sizeof(Buffer))
template<typename T>
class MutantStack : public ft::stack<T>
{
public:
MutantStack() {}
MutantStack(const MutantStack<T>& src) { *this = src; }
MutantStack<T>& operator=(const MutantStack<T>& rhs)
{
this->c = rhs.c;
return *this;
}
~MutantStack() {}
typedef typename ft::stack<T>::container_type::iterator iterator;
iterator begin() { return this->c.begin(); }
iterator end() { return this->c.end(); }
};
int main(int argc, char** argv) {
if (argc != 2)
{
std::cerr << "Usage: ./test seed" << std::endl;
std::cerr << "Provide a seed please" << std::endl;
std::cerr << "Count value:" << COUNT << std::endl;
return 1;
}
time_t start, end;
time(&start);
const int seed = atoi(argv[1]);
srand(seed);
ft::vector<std::string> vector_str;
ft::vector<int> vector_int;
ft::stack<int> stack_int;
ft::vector<Buffer> vector_buffer;
ft::stack<Buffer, std::deque<Buffer> > stack_deq_buffer;
ft::map<int, int> map_int;
for (int i = 0; i < COUNT; i++)
{
vector_buffer.push_back(Buffer());
}
for (int i = 0; i < COUNT; i++)
{
const int idx = rand() % COUNT;
vector_buffer[idx].idx = 5;
}
ft::vector<Buffer>().swap(vector_buffer);
try
{
for (int i = 0; i < COUNT; i++)
{
const int idx = rand() % COUNT;
(void)vector_buffer.at(idx);
std::cerr << "Error: THIS VECTOR SHOULD BE EMPTY!!" <<std::endl;
}
}
catch(const std::exception& e)
{
//NORMAL ! :P
}
for (int i = 0; i < COUNT; ++i)
{
map_int.insert(ft::make_pair(rand(), rand()));
}
int sum = 0;
for (int i = 0; i < 10000; i++)
{
int access = rand();
sum += map_int[access];
}
std::cout << "should be constant with the same seed: " << sum << std::endl;
{
ft::map<int, int> copy = map_int;
}
MutantStack<char> iterable_stack;
for (char letter = 'a'; letter <= 'z'; letter++)
iterable_stack.push(letter);
for (MutantStack<char>::iterator it = iterable_stack.begin(); it != iterable_stack.end(); it++)
{
std::cout << *it;
}
std::cout << std::endl;
/* vector */
ft::vector<int> v1;
print_vector(v1, 1);
ft::vector<int> v2(size_t(8), int(42));
print_vector(v2, 2);
ft::vector<int>::iterator it_tmp1 = v2.begin() + 1;
ft::vector<int>::iterator it_tmp2 = v2.end() - 2;
ft::vector<int> v3(it_tmp1, it_tmp2);
print_vector(v3, 3);
ft::vector<int> v4(v3);
print_vector(v4, 4);
ft::vector<int> v5 = v4;
print_vector(v5, 5);
std::cout << "v1 is empty: "<< v1.empty() << std::endl;
std::cout << "v5 is empty: "<< v5.empty() << std::endl;
std::cout << "v1 has size " << v1.size() << " and capacity " << v1.capacity() << std::endl;
std::cout << "v5 has size " << v5.size() << " and capacity " << v5.capacity() << std::endl;
std::cout << "v1 has max size of " << v1.max_size() << std::endl;
std::cout << "v5 has max size of " << v5.max_size() << std::endl;
v5.resize(20);
print_vector(v5, 5);
std::cout << v5[v5.size() - 1] << std::endl;
std::cout << v5.at(v5.size() - 1) << std::endl;
std::cout << v5.front() << std::endl;
std::cout << v5.back() << std::endl;
ft::vector<int> v6;
v6.assign(7, 100);
print_vector(v6, 6);
ft::vector<int>::iterator v6_it1 = v5.begin() + 3;
ft::vector<int>::iterator v6_it2 = v5.end() - 7;
v6.assign(v6_it1, v6_it2);
print_vector(v6, 6);
ft::vector<int>::allocator_type Allocator = v6.get_allocator();
ft::vector<int> v66(Allocator);
ft::vector<int>::iterator v6_it3 = v6.begin() + 3;
v6.erase(v6_it3);
print_vector(v6, 6);
ft::vector<int>::iterator v6_it4 = v6.begin() + 1;
ft::vector<int>::iterator v6_it5 = v6.begin() + 6;
v6.erase(v6_it4, v6_it5);
print_vector(v6, 6);
ft::vector<int> v7((size_t)10, (int)99);
v7.at(3) = 42;
try
{
v7.at(42) = 666;
} catch (std::out_of_range const& exc) {
std::cout << exc.what() << '\n';
}
v7[4] = 84;
v7[2] = 84;
print_vector(v7, 7);
v7.push_back(66);
v7.push_back(97);
v7.push_back(62);
v7.push_back(28);
v7.push_back(66);
v7.push_back(57);
v7.push_back(12);
ft::vector<int> v8 = v7;
print_vector(v8, 8);
v8.clear();
print_vector(v8, 8);
v8.insert(v8.begin(), 42);
v8.insert(v8.begin(), 41);
v8.insert(v8.begin(), 40);
v8.insert(v8.begin(), (size_t)5, 1);
v8.insert(v8.begin(), (size_t)5, 0);
v8.insert(v8.end(), v7.begin() + 2, v7.begin() + 4);
print_vector(v8, 8);
ft::vector<int>::iterator it1;
ft::vector<int>::iterator it2;
ft::vector<int>::iterator it3;
ft::vector<int>::const_iterator it_const;
ft::vector<int>::reverse_iterator it_r;
ft::vector<int>::const_reverse_iterator it_rconst;
v1.reserve(12);
v1.push_back( 0 );
v1.push_back( 1 );
v1.push_back( 2 );
v1.push_back( 3 );
v1.push_back( 4 );
v1.push_back( 5 );
v1.push_back( 6 );
v1.push_back( 7 );
v1.push_back( 8 );
v1.push_back( 9 );
v1.push_back( 10 );
v1.push_back( 11 );
print_vector(v1, 1);
it1 = v1.begin();
it2 = v1.begin() + 3;
it3 = v1.end() - 1;
std::cout << "it1 = v1.begin()" << std::endl;
std::cout << "it2 = v1.begin() + 3" << std::endl;
std::cout << "it3 = v1.end() - 2;" << std::endl;
std::cout << std::endl;
std::cout << "*(it1): " << *(it1) << std::endl;
std::cout << "(it2 - it1): " << (it2 - it1) << std::endl;
std::cout << "*(it1 + 1): " << *(it1 + 1) << std::endl;
std::cout << "*(1 + it1): " << *(1 + it1) << std::endl;
std::cout << "*(it2 - 1): " << *(it2 - 1) << std::endl;
std::cout << "*(-1 + it2): " << *(-1 + it2) << std::endl;
std::cout << "*(it1++): " << *(it1++) << " -> " << *it1 << std::endl;
std::cout << "*(it2++): " << *(it2++) << " -> " << *it2 << std::endl;
std::cout << "*(++it1): " << *(++it1) << " -> " << *it1 << std::endl;
std::cout << "*(++it2): " << *(++it2) << " -> " << *it2 << std::endl;
std::cout << "*(it1--): " << *(it1--) << " -> " << *it1 << std::endl;
std::cout << "*(it2--): " << *(it2--) << " -> " << *it2 << std::endl;
std::cout << "*(--it1): " << *(--it1) << " -> " << *it1 << std::endl;
std::cout << "*(--it2): " << *(--it2) << " -> " << *it2 << std::endl;
std::cout << "*(it1 += 3): " << *it1 << " -> " << *(it1 += 3) << std::endl;
std::cout << "*(it2 += 3): " << *it2 << " -> " << *(it2 += 3) << std::endl;
std::cout << "*(it1 -= 1): " << *it1 << " -> " << *(it1 -= 1) << std::endl;
std::cout << "*(it2 -= 1): " << *it2 << " -> " << *(it2 -= 1) << std::endl;
ft::reverse_iterator<ft::vector<int>::iterator> it4( v1.rbegin() + 6 );
ft::reverse_iterator<ft::vector<int>::iterator> it5( v1.rbegin() );
std::cout << "*(it4): " << *(it4) << std::endl;
std::cout << "*(it4++): " << *(it4++) << " -> " << *it4 << std::endl;
std::cout << "*(++it4): " << *(++it4) << " -> " << *it4 << std::endl;
std::cout << "*(it4--): " << *(it4--) << " -> " << *it4 << std::endl;
std::cout << "*(--it4): " << *(--it4) << " -> " << *it4 << std::endl;
std::cout << "*(it4 + 3): " << *(it4 + 3) << std::endl;
std::cout << "*(it4 - 3): " << *(it4 - 3) << std::endl;
std::cout << "it4 - it5: " << it4 - it5 << std::endl;
it1 = v1.begin();
it2 = v1.begin() + 3;
ft::vector<int>::const_iterator cit1 = it1;
ft::vector<int>::const_iterator cit2 = it2;
std::cout << "*cit1: " << *cit1 << std::endl;
std::cout << "*cit2: " << *cit2 << std::endl;
// nope!
//cit2 = 67;
// sure
*it1 = 55;
print_vector(v1, 1);
print_vector(v2, 2);
std::cout << std::boolalpha << (v1 == v2) << std::endl;
std::cout << std::boolalpha << (v1 != v2) << std::endl;
std::cout << std::boolalpha << (v1 < v2) << std::endl;
std::cout << std::boolalpha << (v1 > v2) << std::endl;
std::cout << std::boolalpha << (v1 >= v2) << std::endl;
std::cout << std::boolalpha << (v1 <= v2) << std::endl;
v1.swap(v2);
print_vector(v1, 1);
print_vector(v2, 2);
/* map */
ft::map<std::string, int> m1;
std::cout << m1.empty() << std::endl;
m1["CPU"] = 10;
m1["GPU"] = 15;
m1["RAM"] = 20;
std::cout << m1.empty() << std::endl;
print_map(m1, "Initial map: ");
m1["CPU"] = 25;
m1["SSD"] = 30;
print_map(m1, "Updated map: ");
std::cout << "m1[UPS] = " << m1["UPS"] << '\n';
print_map(m1, "Updated map: ");
m1.erase("GPU");
print_map(m1, "After erase: ");
std::cout << "m.size() = " << m1.size() << '\n';
ft::map<std::string, int> m2 = m1;
print_map(m2, "Copy constructed map: ");
std::cout << std::boolalpha << "m1 and m2 are equivalent: " << (m1 == m2) << '\n';
m2["PSU"] = 34;
m2["sPeripherals"] = 12;
print_map(m2, "Updated map: ");
std::cout << std::boolalpha << "m1 and m2 are equivalent: " << (m1 == m2) << '\n';
m1.swap(m2);
print_map(m2, "Swapped map: ");
ft::map<std::string, int> m3(m1);
ft::map<std::string, int>::iterator mit01 = m3.begin();
ft::map<std::string, int>::iterator mit02 = m3.end();
mit02--;
ft::map<std::string, int>::reverse_iterator rmit01 = m3.rbegin();
ft::map<std::string, int>::reverse_iterator rmit02 = m3.rend();
rmit02++;
print_map(m3, "m3: ");
std::cout << "Begin --- Key: " << mit01->first << " | Value: " << mit01->second << std::endl;
std::cout << "End --- Key: " << mit02->first << " | Value: " << mit02->second << std::endl;
std::cout << "Reverse Begin --- Key: " << rmit01->first << " | Value: " << rmit01->second << std::endl;
std::cout << "Reverse End --- Key: " << rmit02->first << " | Value: " << rmit02->second << std::endl;
ft::map<std::string, int> m4(mit01, mit02);
print_map(m4, "m4: ");
std::cout << "m4 size = " << m4.size() << std::endl;
std::cout << "m4 size = " << m4.max_size() << std::endl;
ft::map<std::string, int>::allocator_type mapalloc = m4.get_allocator();
ft::map<std::string, int> m5 = m4;
m2["Mousepad"] = 5;
m5.insert(m2.begin(), m2.end());
print_map(m2, "m2: ");
print_map(m5, "m5: ");
ft::map<std::string, int> m6 = m5;
m6.erase(m6.begin());
print_map(m6, "m6: ");
print_map(m2, "m2: ");
std::cout << "M6 size is " << m6.size() << std::endl;
m6.erase(m6.begin(), m6.end());
print_map(m6, "erased: m6: ");
m6.swap(m5);
print_map(m6, "m6: ");
print_map(m5, "swapped with m5: ");
std::cout << m5.count("RAM") << std::endl;
std::cout << m5.count("HDD") << std::endl;
if (m5.find("RAM") == m5.end())
std::cout << "RAM not found" << std::endl;
if (m5.find("UPS") == m5.end())
std::cout << "UPS not found" << std::endl;
print_map(m5, "m5: ");
ft::pair<ft::map<std::string, int>::iterator, ft::map<std::string, int>::iterator> range00 = m6.equal_range("PSU");
std::cout << range00.first->first << " = " << range00.first->second << std::endl;
std::cout << range00.second->first << " = " << range00.second->second << std::endl;
std::cout << (*m6.lower_bound("PSU")).first << " = " << (*m6.lower_bound("PSU")).second << std::endl;
std::cout << (*m6.upper_bound("PSU")).first << " = " << (*m6.upper_bound("PSU")).second << std::endl;
std::cout << "1) " << std::boolalpha << (m3 == m5) << std::endl;
std::cout << "2) " << std::boolalpha << (m3 != m5) << std::endl;
std::cout << "3) " << std::boolalpha << (m3 < m5) << std::endl;
std::cout << "4) " << std::boolalpha << (m3 > m5) << std::endl;
std::cout << "5) " << std::boolalpha << (m3 >= m5) << std::endl;
std::cout << "6) " << std::boolalpha << (m3 <= m5) << std::endl;
ft::map<std::string, int>::key_compare key_comp_map = m1.key_comp();
ft::map<std::string, int>::value_compare val_comp_map = m1.value_comp();
std::cout << std::boolalpha << "Python is lexicographically less than C: " << key_comp_map("Python", "C") << '\n';
std::cout << (std::string("Python") < std::string("C")) << '\n';
ft::map<std::string, int>::value_type value1_map = ft::make_pair("C", 2);
ft::map<std::string, int>::value_type value2_map = ft::make_pair("C++", 7);
std::cout << std::boolalpha << "C is lexicographically less than C++: " << val_comp_map(value1_map, value2_map) << '\n';
std::cout << "C comp C++ : " << (std::string("C") < std::string("C++")) << '\n';
ft::map<std::string, int>::iterator it01 = m1.find("SSD");
(void)it01;
std::cout << "Key \"SSD\" is in the map: " << m1.count("SSD") << '\n';
std::cout << "Key \"Stereo\" is in the map: " << m1.count("Stereo") << '\n';
m1.clear();
std::cout << std::boolalpha << "Map is empty: " << m1.empty() << '\n';
ft::map<std::string, int>::reverse_iterator mrev2;
ft::map<std::string, int>::const_iterator mci1;
ft::map<std::string, int>::iterator mi1;
std::cout << m1.empty() << std::endl;
m1["CPU"] = 10;
m1["GPU"] = 15;
m1["RAM"] = 20;
std::cout << m1.empty() << std::endl;
print_map(m1, "Initial map: ");
m1["CPU"] = 25;
m1["SSD"] = 30;
print_map(m1, "Updated map: ");
std::cout << "m1[UPS] = " << m1["UPS"] << '\n';
print_map(m1, "Updated map: ");
mci1 = m1.begin();
std::cout << "const iterator" << std::endl;
while (mci1 != m1.end())
{
std::cout << (*mci1).first << std::endl;
mci1++;
}
std::cout << "iterator" << std::endl;
mi1 = m1.begin();
while (mi1 != m1.end())
{
std::cout << (*mi1).first << std::endl;
mi1++;
}
std::cout << "reverse_iterator" << std::endl;
mrev2 = m1.rbegin();
while (mrev2 != m1.rend())
{
std::cout << (*mrev2).first << std::endl;
mrev2++;
}
std::cout << "const_reverse_iterator" << std::endl;
ft::map<std::string, int>::const_reverse_iterator mrev1;
ft::map<std::string, int>::const_reverse_iterator mrev3;
mrev1 = m1.rbegin();
mrev3 = m1.rend();
while (mrev1 != m1.rend())
{
std::cout << (*mrev1).first << std::endl;
mrev1++;
}
/* set */
std::cout << std::endl << "SET" << std::endl << std::endl;
ft::set<std::string> s1;
std::cout << s1.empty() << std::endl;
s1.insert("CPU");
s1.insert("GPU");
s1.insert("RAM");
std::cout << s1.empty() << std::endl;
print_set(s1, "Initial set: ");
s1.insert("CPU");
s1.insert("SSD");
print_set(s1, "Updated set: ");
s1.erase("GPU");
print_set(s1, "After erase: ");
std::cout << "m.size() = " << s1.size() << '\n';
ft::set<std::string> s2 = s1;
print_set(s2, "Copy constructed set: ");
std::cout << std::boolalpha << "s1 and s2 are equivalent: " << (s1 == s2) << '\n';
s2.insert("PSU");
s2.insert("Peripherals");
print_set(s2, "Updated set: ");
std::cout << std::boolalpha << "s1 and s2 are equivalent: " << (s1 == s2) << '\n';
s1.swap(s2);
print_set(s2, "Swapped set: ");
ft::set<std::string>::key_compare key_comp_set = s1.key_comp();
ft::set<std::string>::value_compare val_comp_set = s1.value_comp();
std::cout << std::boolalpha << "Python is lexicographically less than C: " << key_comp_set("Python", "C") << '\n';
std::cout << (std::string("Python") < std::string("C")) << '\n';
ft::set<std::string>::value_type value1_set = (std::string)"C";
ft::set<std::string>::value_type value2_set = (std::string)"C++";
std::cout << std::boolalpha << "C is lexicographically less than C++: " << val_comp_set(value1_set, value2_set) << '\n';
std::cout << (std::string("C") < std::string("C++")) << '\n';
ft::set<std::string>::iterator it01_set = s1.find("SSD");
(void)it01_set;
std::cout << "Key \"SSD\" is in the set: " << s1.count("SSD") << '\n';
std::cout << "Key \"Stereo\" is in the set: " << s1.count("Stereo") << '\n';
ft::set<std::string> s3(s1);
ft::set<std::string>::iterator sit01 = s3.begin();
ft::set<std::string>::iterator sit02 = s3.end();
sit02--;
ft::set<std::string>::reverse_iterator rsit01 = s3.rbegin();
ft::set<std::string>::reverse_iterator rsit02 = s3.rend();
rsit02--;
print_set(s3, "s3: ");
std::cout << "Begin --- Key: " << *sit01 << std::endl;
std::cout << "End --- Key: " << *sit02 << std::endl;
std::cout << "Reverse Begin --- Key: " << *rsit01 << std::endl;
std::cout << "Reverse End --- Key: " << *rsit02 << std::endl;
ft::set<std::string> s4(sit01, sit02);
print_set(s4, "s4: ");
std::cout << "s4 size = " << s4.size() << std::endl;
std::cout << "s4 size = " << s4.max_size() << std::endl;
ft::set<std::string>::allocator_type setalloc = s4.get_allocator();
ft::set<std::string> s5 = s4;
s5.insert(s2.begin(), s2.end());
print_set(s2, "s2: ");
print_set(s5, "s5: ");
ft::set<std::string> s6 = s5;
s6.erase(s6.begin());
print_set(s6, "s6: ");
s6.erase(s6.begin(), s6.end());
print_set(s6, "s6: ");
s6.swap(s5);
print_set(s6, "s6: ");
std::cout << s5.count("RAM") << std::endl;
std::cout << s5.count("HDD") << std::endl;
//Dangerous !! find will return end() iterator if nothing was found
//trying to dereference end() will cause undefined behavior (UB)
std::cout << (*s6.find("RAM")) << std::endl;
std::cout << (*s6.find("PSU")) << std::endl;
//better this way !
if (s6.find("USP") == s6.end())
std::cout << "USP not found" << std::endl;
else
std::cout << "USP found" << std::endl;
//same here, equal_range returns a pair of iterators, take care that you
//are not going to try dereferencing end()
ft::pair<ft::set<std::string>::iterator, ft::set<std::string>::iterator> srange00 = s6.equal_range("PSU");
std::cout << *(srange00.first) << " = " << *(srange00.first) << std::endl;
std::cout << *(srange00.first) << " = " << *(srange00.second) << std::endl;
std::cout << (*m6.lower_bound("PSU")).first << " = " << (*m6.lower_bound("PSU")).second << std::endl;
std::cout << (*m6.upper_bound("PSU")).first << " = " << (*m6.upper_bound("PSU")).second << std::endl;
std::cout << std::boolalpha << (s3 == s5) << std::endl;
std::cout << std::boolalpha << (s3 != s5) << std::endl;
std::cout << std::boolalpha << (s3 < s5) << std::endl;
std::cout << std::boolalpha << (s3 > s5) << std::endl;
std::cout << std::boolalpha << (s3 >= s5) << std::endl;
std::cout << std::boolalpha << (s3 <= s5) << std::endl;
s1.clear();
std::cout << std::boolalpha << "Set is empty: " << s1.empty() << '\n';
ft::set<std::string>::reverse_iterator srev2;
ft::set<std::string>::const_iterator sci1;
ft::set<std::string>::iterator si1;
std::cout << s1.empty() << std::endl;
s1.insert("CPU");
s1.insert("GPU");
s1.insert("RAM");
std::cout << s1.empty() << std::endl;
print_set(s1, "Initial map: ");
sci1 = s1.begin();
std::cout << "const iterator" << std::endl;
while (sci1 != s1.end())
{
std::cout << (*sci1) << std::endl;
sci1++;
}
std::cout << "iterator" << std::endl;
si1 = s1.begin();
while (si1 != s1.end())
{
std::cout << (*si1) << std::endl;
si1++;
}
std::cout << "reverse_iterator" << std::endl;
srev2 = s1.rbegin();
while (srev2 != s1.rend())
{
std::cout << (*srev2) << std::endl;
srev2++;
}
std::cout << "const_reverse_iterator" << std::endl;
ft::set<std::string>::const_reverse_iterator srev1;
ft::set<std::string>::const_reverse_iterator srev3;
srev1 = s1.rbegin();
srev3 = s1.rend();
while (srev1 != s1.rend()) //rev3)
{
std::cout << (*srev1) << std::endl;
srev1++;
}
/* stack */
ft::stack<int> stack0;
stack0.push(1);
stack0.push(2);
stack0.push(3);
stack0.push(4);
print_stack(stack0, 0);
ft::stack<int> stack1 = stack0;
print_stack(stack1, 1);
ft::stack<int> stack2(stack1);
print_stack(stack2, 2);
stack1.pop();
stack2.pop();
stack2.pop();
std::cout << stack0.top() << std::endl;
std::cout << stack1.top() << std::endl;
std::cout << stack2.top() << std::endl;
stack2.pop();
stack2.pop();
std::cout << std::boolalpha << "Stack2 is empty: " << stack2.empty() << '\n';
std::cout << std::boolalpha << "Stack0 is empty: " << stack0.empty() << '\n';
std::cout << "Stack2 size: " << stack2.size() << '\n';
std::cout << "Stack0 size: " << stack0.size() << '\n';
std::cout << "1) " << std::boolalpha << (stack0 == stack1) << std::endl;
std::cout << "2) " << std::boolalpha << (stack0 != stack1) << std::endl;
std::cout << "3) " << std::boolalpha << (stack0 < stack1) << std::endl;
std::cout << "4) " << std::boolalpha << (stack0 > stack1) << std::endl;
std::cout << "5) " << std::boolalpha << (stack0 >= stack1) << std::endl;
std::cout << "6) " << std::boolalpha << (stack0 <= stack1) << std::endl;
time(&end);
double diff = difftime(end, start);
int tme = static_cast<int> (diff);
std::cout << "Executed in " << tme << " seconds" << std::endl;
return (0);
}