-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_vector.cpp
482 lines (448 loc) · 15.9 KB
/
main_vector.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main_vector.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mbari <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/22 21:35:47 by mbari #+# #+# */
/* Updated: 2022/03/22 22:50:34 by mbari ### ########.fr */
/* */
/* ************************************************************************** */
#include "containers/vector/vector.hpp"
#include <vector>
#ifndef PH
#define PH ft
#endif
int main(){
// // *************************************************************************
// //NOTE - Vector
// // *************************************************************************
//NOTE - Constructor
{
std::cout << "------------- Library -------------" << std::endl;
PH::vector<int> first1;
PH::vector<int> second (4,100);
PH::vector<int> third (second.begin(),second.end());
PH::vector<int> fourth (third);
int myints[] = {16,2,77,29};
PH::vector<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );
std::cout << "The contents of fifth are:";
for (PH::vector<int>::iterator it = fifth.begin(); it != fifth.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
}
{
//NOTE - Assign content (operator=)
std::cout << "------------- Library -------------" << std::endl;
PH::vector<int> foo1 (3,0);
PH::vector<int> bar1 (5,0);
bar1 = foo1;
foo1 = PH::vector<int>();
std::cout << "Size of foo: " << int(foo1.size()) << '\n';
std::cout << "Size of bar: " << int(bar1.size()) << '\n';
}
{
//NOTE - Return iterator to beginning
std::cout << "------------- Library -------------" << std::endl;
PH::vector<int> myvector1;
for (int i=1; i<=5; i++) myvector1.push_back(i);
std::cout << "myvector contains:";
for (PH::vector<int>::iterator it = myvector1.begin() ; it != myvector1.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
}
{
//NOTE - Return iterator to end
std::cout << "------------- Library -------------" << std::endl;
PH::vector<int> myvector1;
for (int i=1; i<=5; i++) myvector1.push_back(i);
std::cout << "myvector contains:";
for (PH::vector<int>::iterator it = myvector1.begin() ; it != myvector1.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
}
{
// NOTE - Return reverse iterator to reverse beginning
std::cout << "------------- Library -------------" << std::endl;
PH::vector<int> myvector1 (5); // 5 default-constructed ints
int i1=0;
PH::vector<int>::reverse_iterator rit1 = myvector1.rbegin();
for (; rit1!= myvector1.rend(); ++rit1)
*rit1 = ++i1;
std::cout << "myvector contains:";
for (PH::vector<int>::iterator it1 = myvector1.begin(); it1 != myvector1.end(); ++it1)
std::cout << ' ' << *it1;
std::cout << '\n';
}
{
// NOTE - Return reverse iterator to reverse end
std::cout << "------------- Library -------------" << std::endl;
PH::vector<int> myvector1 (5);
PH::vector<int>::reverse_iterator rit1 = myvector1.rbegin();
int i1=0;
for (rit1 = myvector1.rbegin(); rit1!= myvector1.rend(); ++rit1)
*rit1 = ++i1;
std::cout << "myvector contains:";
for (PH::vector<int>::iterator it1 = myvector1.begin(); it1 != myvector1.end(); ++it1)
std::cout << ' ' << *it1;
std::cout << '\n';
}
{
// NOTE - Return size
std::cout << "------------- Library -------------" << std::endl;
PH::vector<int> myints1;
std::cout << "0. size: " << myints1.size() << '\n';
for (int i=0; i<10; i++) myints1.push_back(i);
std::cout << "1. size: " << myints1.size() << '\n';
myints1.insert (myints1.end(),10,100);
std::cout << "2. size: " << myints1.size() << '\n';
myints1.pop_back();
std::cout << "3. size: " << myints1.size() << '\n';
}
{
// NOTE - Return maximum size
std::cout << "------------- Library -------------" << std::endl;
PH::vector<int> myvector1;
for (int i=0; i<100; i++) myvector1.push_back(i);
std::cout << "size: " << myvector1.size() << "\n";
std::cout << "capacity: " << myvector1.capacity() << "\n";
std::cout << "max_size: " << myvector1.max_size() << "\n";
}
{
// NOTE - Change size
std::cout << "------------- Library -------------" << std::endl;
PH::vector<int> myvector1;
for (int i=1;i<10;i++) myvector1.push_back(i);
myvector1.resize(9);
myvector1.resize(12,100);
myvector1.resize(12);
std::cout << myvector1.size() << std::endl;
std::cout << "myvector contains:";
for (size_t i=0;i<myvector1.size();i++)
std::cout << ' ' << myvector1[i];
std::cout << '\n';
}
{
// NOTE - Return size of allocated storage capacity
std::cout << "------------- Library -------------" << std::endl;
PH::vector<int> myvector1;
for (int i=0; i<100; i++) myvector1.push_back(i);
std::cout << "size: " << (int) myvector1.size() << '\n';
std::cout << "capacity: " << (int) myvector1.capacity() << '\n';
std::cout << "max_size: " << (int) myvector1.max_size() << '\n';
}
{
// NOTE - Test whether vector is empty
std::cout << "------------- Library -------------" << std::endl;
PH::vector<int> myvector1;
int sum1 (0);
for (int i=1;i<=10;i++) myvector1.push_back(i);
while (!myvector1.empty())
{
sum1 += myvector1.back();
myvector1.pop_back();
}
std::cout << "total: " << sum1 << '\n';
}
{
// NOTE - Request a change in capacity
std::cout << "------------- Library -------------" << std::endl;
PH::vector<int>::size_type sz1;
PH::vector<int> foo1;
sz1 = foo1.capacity();
std::cout << "making foo grow:\n";
for (int i=0; i<100; ++i) {
foo1.push_back(i);
if (sz1!=foo1.capacity()) {
sz1 = foo1.capacity();
std::cout << "capacity changed: " << sz1 << '\n';
}
}
PH::vector<int> bar1;
sz1 = bar1.capacity();
bar1.reserve(100); // this is the only difference with foo above
std::cout << "making bar grow:\n";
for (int i=0; i<100; ++i) {
bar1.push_back(i);
if (sz1!=bar1.capacity()) {
sz1 = bar1.capacity();
std::cout << "capacity changed: " << sz1 << '\n';
}
}
}
{
// NOTE - Access element
std::cout << "------------- Library -------------" << std::endl;
PH::vector<int> myvector1 (10);
PH::vector<int>::size_type sz1 = myvector1.size();
for (unsigned i=0; i<sz1; i++) myvector1[i]=i;
for (unsigned i=0; i<sz1/2; i++)
{
int temp1;
temp1 = myvector1[sz1-1-i];
myvector1[sz1-1-i]=myvector1[i];
myvector1[i]=temp1;
}
std::cout << "myvector contains:";
for (unsigned i=0; i<sz1; i++)
std::cout << ' ' << myvector1[i];
std::cout << '\n';
}
{
// NOTE - Access element
std::cout << "------------- Library -------------" << std::endl;
PH::vector<int> myvector1 (10);
for (unsigned i=0; i<myvector1.size(); i++)
myvector1.at(i)=i;
std::cout << "myvector contains:";
for (unsigned i=0; i<myvector1.size(); i++)
std::cout << ' ' << myvector1.at(i);
std::cout << '\n';
}
{
// NOTE - Access first element
std::cout << "------------- Library -------------" << std::endl;
PH::vector<int> myvector1;
myvector1.push_back(78);
myvector1.push_back(16);
myvector1.front() -= myvector1.back();
std::cout << "myvector.front() is now " << myvector1.front() << '\n';
}
{
// NOTE - Access last element
std::cout << "------------- Library -------------" << std::endl;
PH::vector<int> myvector1;
myvector1.push_back(10);
while (myvector1.back() != 0)
{
myvector1.push_back ( myvector1.back() -1 );
}
std::cout << "myvector contains:";
for (unsigned i=0; i<myvector1.size() ; i++)
std::cout << ' ' << myvector1[i];
std::cout << '\n';
}
{
// NOTE - Assign vector content
std::cout << "------------- Library -------------" << std::endl;
PH::vector<int> first1;
PH::vector<int> second1;
PH::vector<int> third1;
first1.assign (7,100);
PH::vector<int>::iterator it1;
it1=first1.begin()+1;
second1.assign (it1,first1.end()-1);
int myints1[] = {1776,7,4};
third1.assign (myints1,myints1+3);
std::cout << "Size of first: " << int (first1.size()) << '\n';
std::cout << "Size of second: " << int (second1.size()) << '\n';
std::cout << "Size of third: " << int (third1.size()) << '\n';
}
// {
// // NOTE - Add element at the end
// std::cout << "------------- Library -------------" << std::endl;
// PH::vector<int> myvector1;
// int myint1;
// std::cout << "Please enter some integers (enter 0 to end):\n";
// do {
// std::cin >> myint1;
// myvector1.push_back (myint1);
// } while (myint1);
// std::cout << "myvector stores " << int(myvector1.size()) << " numbers.\n";
// }
{
// NOTE - Delete last element
std::cout << "------------- Library -------------" << std::endl;
PH::vector<int> myvector1;
int sum1 (0);
myvector1.push_back (100);
myvector1.push_back (200);
myvector1.push_back (300);
while (!myvector1.empty())
{
sum1+=myvector1.back();
myvector1.pop_back();
}
std::cout << "The elements of myvector add up to " << sum1 << '\n';
}
{
// NOTE - Clear content
std::cout << "------------- Library -------------" << std::endl;
PH::vector<int> myvector1;
myvector1.push_back (100);
myvector1.push_back (200);
myvector1.push_back (300);
std::cout << "myvector contains:";
for (unsigned i=0; i<myvector1.size(); i++)
std::cout << ' ' << myvector1[i];
std::cout << '\n';
std::cout << myvector1.size() << std::endl;
std::cout << myvector1.capacity() << std::endl;
myvector1.clear();
std::cout << myvector1.size() << std::endl;
std::cout << myvector1.capacity() << std::endl;
myvector1.push_back (1101);
myvector1.push_back (2202);
std::cout << "myvector contains:";
for (unsigned i=0; i<myvector1.size(); i++)
std::cout << ' ' << myvector1[i];
std::cout << '\n';
}
{
// NOTE - Insert elements
std::cout << "------------- Library -------------" << std::endl;
PH::vector<int> myvector1(3, 100);
std::cout << "myvector size : " << myvector1.size() << std::endl;
std::cout << "myvector capacity: " << myvector1.capacity() << std::endl;
PH::vector<int>::iterator it1;
it1 = myvector1.begin();
it1 = myvector1.insert ( it1, 200 );
myvector1.insert(it1, 2, 300);
it1 = myvector1.begin();
PH::vector<int> anothervector1 (2,400);
std::cout << "myvector size : " << myvector1.size() << std::endl;
std::cout << "myvector capacity : " << myvector1.capacity() << std::endl;
myvector1.insert (it1+2,anothervector1.begin(),anothervector1.end());
std::cout << "myvector size : " << myvector1.size() << std::endl;
std::cout << "myvector capacity : " << myvector1.capacity() << std::endl;
int myarray1 [] = { 501,502,503 };
myvector1.insert (myvector1.begin(), myarray1, myarray1+3);
std::cout << "myvector contains:";
for (it1=myvector1.begin(); it1<myvector1.end(); it1++)
std::cout << ' ' << *it1;
std::cout << '\n';
}
{
PH::vector<int> vec1;
vec1.push_back(10);
vec1.push_back(20);
vec1.push_back(30);
vec1.push_back(40);
PH::vector<int>::iterator it1 = vec1.insert(vec1.begin(), 3);
vec1.insert(it1, 2);
int i1 = 2;
it1 = vec1.insert(vec1.begin() + i1, 7);
std::cout << "The vector elements are: ";
for (PH::vector<int>::iterator it1 = vec1.begin(); it1 != vec1.end(); ++it1)
std::cout << *it1 << " ";
}
{
// NOTE - Erase elements
std::cout << "------------- Library -------------" << std::endl;
PH::vector<int> myvector1;
for (int i=1; i<=10; i++) myvector1.push_back(i);
std::cout << myvector1.size() << std::endl;
std::cout << myvector1.capacity() << std::endl;
std::cout << "myvector contains:";
for (unsigned i=0; i<myvector1.size(); ++i)
std::cout << ' ' << myvector1[i];
std::cout << std::endl;
myvector1.erase (myvector1.begin() - 1);
std::cout << "myvector contains:";
for (unsigned i=0; i<myvector1.size(); ++i)
std::cout << ' ' << myvector1[i];
std::cout << std::endl;
myvector1.erase (myvector1.begin(),myvector1.begin()+3);
std::cout << myvector1.size() << std::endl;
std::cout << myvector1.capacity() << std::endl;
std::cout << "myvector contains:";
for (unsigned i=0; i<myvector1.size(); ++i)
std::cout << ' ' << myvector1[i];
std::cout << std::endl;
}
{
// NOTE - Erase elements
std::cout << "------------- Library -------------" << std::endl;
std::vector<int> myvector1;
for (int i=1; i<=10; i++) myvector1.push_back(i);
std::cout << myvector1.size() << std::endl;
std::cout << myvector1.capacity() << std::endl;
myvector1.erase (myvector1.begin() - 1);
myvector1.erase (myvector1.begin(),myvector1.begin()+3);
std::cout << myvector1.size() << std::endl;
std::cout << myvector1.capacity() << std::endl;
std::cout << "myvector contains:";
for (unsigned i=0; i<myvector1.size(); ++i)
std::cout << ' ' << myvector1[i];
std::cout << std::endl;
}
{
// NOTE - Swap content
std::cout << "------------- Library -------------" << std::endl;
PH::vector<int> foo1(3, 100); // three ints with a value of 100
PH::vector<int> bar1(5, 200); // five ints with a value of 200
std::cout << "Size foo1 before " << foo1.size() << std::endl;
std::cout << "Capa foo1 before " << foo1.capacity() << std::endl;
std::cout << "Size bar1 before " << bar1.size() << std::endl;
std::cout << "Capa bar1 before " << bar1.capacity() << std::endl;
foo1.swap(bar1);
std::cout << "Size foo1 after " << foo1.size() << std::endl;
std::cout << "Capa foo1 after " << foo1.capacity() << std::endl;
std::cout << "Size bar1 after " << bar1.size() << std::endl;
std::cout << "Capa bar1 after " << bar1.capacity() << std::endl;
std::cout << "foo contains:";
for (unsigned i=0; i<foo1.size(); i++)
std::cout << ' ' << foo1[i];
std::cout << '\n';
std::cout << "bar contains:";
for (unsigned i=0; i<bar1.size(); i++)
std::cout << ' ' << bar1[i];
std::cout << '\n';
}
{
// NOTE - Get allocator
std::cout << "------------- Library -------------" << std::endl;
PH::vector<int> myvector;
int * p;
unsigned int i;
p = myvector.get_allocator().allocate(5);
for (i=0; i<5; i++) myvector.get_allocator().construct(&p[i],i);
std::cout << "The allocated array contains:";
for (i=0; i<5; i++) std::cout << ' ' << p[i];
std::cout << '\n';
for (i=0; i<5; i++) myvector.get_allocator().destroy(&p[i]);
myvector.get_allocator().deallocate(p,5);
}
{
// NOTE - Relational operators for vector
PH::vector<int> foo (1,200);
std::cout << "------------- Library -------------" << std::endl;
PH::vector<int> bar (0);
if (foo==bar) std::cout << "foo and bar are equal\n";
if (foo!=bar) std::cout << "foo and bar are not equal\n";
if (foo< bar) std::cout << "foo is less than bar\n";
if (foo> bar) std::cout << "foo is greater than bar\n";
if (foo<=bar) std::cout << "foo is less than or equal to bar\n";
if (foo>=bar) std::cout << "foo is greater than or equal to bar\n";
}
{
// NOTE - Exchange contents of vectors
std::cout << "------------- Library -------------" << std::endl;
PH::vector<int> foo (3,100); // three ints with a value of 100
PH::vector<int> bar (5,200); // five ints with a value of 200
// foo(bar);//NOTE:
std::cout << "foo contains:";
for (PH::vector<int>::iterator it = foo.begin(); it!=foo.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
std::cout << "bar contains:";
for (PH::vector<int>::iterator it = bar.begin(); it!=bar.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
}
{
//NOTE - compare it and const-it
std::cout << "------------- Library -------------" << std::endl;
PH::vector<int> foo (3,100); // three ints with a value of 100
PH::vector<int> bar (5,200); // five ints with a value of 200
std::cout << "foo contains:";
for (PH::vector<int>::iterator it = foo.begin(); it!=foo.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
std::cout << "bar contains:";
for (PH::vector<int>::iterator it = bar.begin(); it!=bar.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
}
}