-
Notifications
You must be signed in to change notification settings - Fork 160
/
vector.cpp
455 lines (406 loc) · 12.3 KB
/
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
// # vector
//
// Dynamic array: https://en.wikipedia.org/wiki/Dynamic_array
#include "common.hpp"
int main() {
// Create
{
// Empty
{
std::vector<int> v;
// C++11 initializer lists:
std::vector<int> v1{};
assert(v == v1);
}
// Given size
{
std::vector<int> v(3);
assert(v.size() == 3);
}
// Fill constructor.
//
// Make a `std::vector` with n copies of a single value.
{
// Copies of given object.
{
assert(std::vector<int>(3, 2) == std::vector<int>({2, 2, 2}));
}
// Default constructed objects. int = 0.
{
assert(std::vector<int>(3) == std::vector<int>({0, 0, 0}));
}
}
// Range copy.
{
std::vector<int> v{0, 1, 2};
std::vector<int> v1(v.begin(), v.end());
assert(v == v1);
}
// From existing array.
{
int myints[]{0, 1, 2};
std::vector<int> v(myints, myints + sizeof(myints) / sizeof(int));
std::vector<int> v1 = {0, 1, 2};
assert(v == v1);
}
}
// Vectors have order.
{
std::vector<int> v{0, 1, 2};
std::vector<int> v1{2, 1, 0};
assert(v != v1);
}
// # Data
//
// Storage is required to be contiguous by TR1:
// http://stackoverflow.com/questions/849168/are-stdvector-elements-guaranteed-to-be-contiguous
//
// C++11 introduces the `data()` method which returns a pointer to the first element.
// It works even if the vector is empty.
// http://stackoverflow.com/questions/6485496/how-to-get-stdvector-pointer-to-the-raw-data
//
// Before C++11, `&v[0]` works for non-empty vectors.
//
// `vector<bool>` as usual is an exception.
{
std::vector<int> v{0, 1, 2};
assert(&v[0] == v.data());
// True because contiguous:
assert(v.data()[1] == v[1]);
}
// size methods
{
// # size
//
// # length of vector
//
// # size_type
//
// Number of elements in std::vector.
//
// This has type std::vector<X>::size_type
{
std::vector<int> v;
assert(v.size() == 0);
v.push_back(0);
assert(v.size() == 1);
}
// # resize
//
// If larger than current size, append given element at end default initialized.
//
// If smaller than current size, remove elements from end.
{
// Reduce size
{
std::vector<int> v{0, 1};
v.resize(1);
assert((v == std::vector<int>{0}));
}
// Increase size
{
// Using default constructor objects.
{
std::vector<int> v{1};
v.resize(3);
assert((v == std::vector<int>{1, 0, 0}));
}
// Using copies of given object.
{
std::vector<int> v{1};
v.resize(3, 2);
assert((v == std::vector<int>{1, 2, 2}));
}
}
}
}
// Capacity methods.
{
// # capacity
//
// Get currently allocated size.
//
// Different from size, which is the number of elements in the std::vector!
//
// At least as large as size.
//
// Likely to be a power of 2 on most implementations.
{
std::vector<int> v;
v.push_back(0);
v.push_back(1);
v.push_back(2);
assert(v.capacity() >= 3);
std::cout << "capacity = " << v.capacity() << std::endl;
}
// # max_size: estimate of what your OS allows you to allocate
{
std::cout << "max_size (MiB) = " << std::vector<int>().max_size() / (1 << 20) << std::endl;
}
// # reserve: increase allocated size if larger than current size.
{
std::vector<int> v;
v.reserve(3);
assert(v.capacity() >= 3);
// size() is untouched
assert(v.empty());
}
#if __cplusplus >= 201103L
// # shrink_to_fit
{
std::vector<int> v{0, 1};
v.reserve(4);
v.shrink_to_fit();
assert(v.capacity() == 2);
}
#endif
}
// `std::vector` stores copies of elements, not references.
{
std::string s = "abc";
std::vector<std::string> v{s};
v[0][0] = '0';
assert(v[0] == "0bc");
assert(s == "abc");
}
// Modify.
{
{
std::vector<int> v;
v = {0};
v = {0, 1};
assert((v == std::vector<int>{0, 1}));
}
// # push_back
//
// # append
//
// Push to the end of the std::vector.
//
// Amortized time O(1), but may ocassionaly make the std::vector grow,
// which may required a full data copy to a new location if the
// current backing array cannot grow.
//
// # push_front
//
// Does not exist for std::vector, as it would always be too costly (requires to move
// each element forward.) Use deque if you need that.
{
std::vector<int> v;
std::vector<int> v1;
v.push_back(0);
v1 = {0};
assert(v == v1);
v.push_back(1);
v1 = {0, 1};
assert(v == v1);
// push_back makes copies with assign `=`
//
// If you want references, use pointers, or even better, auto_ptr.
{
std::vector<std::string> v;
std::string s = "abc";
v.push_back(s);
v[0][0] = '0';
assert(v[0] == "0bc");
//s was not changed
assert(s == "abc");
}
}
// # pop_back
//
// Remove last element from std::vector.
//
// No return val. Rationale: http://stackoverflow.com/questions/12600330/pop-back-return-value
{
std::vector<int> v{0, 1};
v.pop_back();
assert(v == std::vector<int>{0});
v.pop_back();
assert(v == std::vector<int>{});
}
// # insert
//
// This operation is inneficient for `std::vector` if it is not done at the end.
//
// # concatenate
//
// The range form of insert can be used to append one vector to anoter.
{
// Single element form.
{
std::vector<int> v = {0,1};
std::vector<int> v1;
v.insert(v.begin(), -1);
v1 = {-1, 0, 1};
assert(v == v1);
v.insert(v.end(), 2);
v1 = {-1, 0, 1, 2};
assert(v == v1);
}
// Range form.
{
std::vector<int> v = {0,1};
std::vector<int> v1 = {2,3};
v.insert(v.end(), v1.begin(), v1.end());
assert((v == std::vector<int>{0, 1, 2, 3}));
}
}
// # erase
//
// Remove given elements from container given iterators to those elements.
//
// This operation is inefficient for std::vectors,
// since it may mean reallocation and therefore up to $O(n)$ operations.
//
// Returns a pointer to the new location of the element next to the last removed element.
{
// Single element
{
std::vector<int> v{0, 1, 2};
auto it = v.erase(v.begin() + 1);
assert((v == std::vector<int>{0, 2}));
assert(*it == 2);
}
// Range
{
std::vector<int> v{0, 1, 2, 3};
auto it = v.erase(v.begin() + 1, v.end() - 1);
assert((v == std::vector<int>{0, 3}));
assert(*it == 3);
}
}
// # remove
//
// Helper to remove all elements that compare equal to a value from container.
//
// Does not actually remove the elements: only ensures that the beginning of the range
// does not contain the item to be removed.
//
// Ex:
//
// 0, 1, 0, 2, 0, 1
//
// Value to remove: `0`
//
// Range to remove from:
//
// 0, 1, 0, 2, 0, 1
// ----------
//
// After the remove:
//
// 1, 2, X, Y, 0, 1
// ----------
//
// where `X` and `Y` are trash, and not necessarily 0!
//
// To actually remove the items, an `erase` is needed after remove
// because `remove` is not a class method and thus cannot remove items from a container.
//
// This is called the erase and remove idiom:
// https://en.wikipedia.org/wiki/Erase%E2%80%93remove_idiom
//
// After a remove the container becomes:
//
// 1, 2, 0, 1
{
std::vector<int> v{0, 1, 0, 2, 0, 1};
auto end = std::next(v.end(), -2);
v.erase(std::remove(v.begin(), end, 0), end);
assert((v == std::vector<int>{1, 2, 0, 1}));
}
// # clear
//
// Make the vector empty.
{
std::vector<int> v{0, 1, 2};
v.clear();
assert(v.empty());
}
// # vector to string
//
// No built-in way.
//
// http://stackoverflow.com/questions/4850473/pretty-print-c-stl-containers
// 190 votes on question, 30 on top answer! Come on C++!
//
// http://stackoverflow.com/questions/1430757/c-vector-to-string?lq=1
}
// Random access is O(1) since array backed
{
std::vector<int> v{0, 1, 2};
// First element:
assert(v.front() == 0);
assert(v.front() == v[0]);
// Last element:
assert(v.back() == 2);
// Nth element:
v[0] = 1;
assert(v[0] == 1);
// BAD: just like array overflow will not change std::vector size,
// and is unlikely to give an error
{
//v1[2] = 2;
}
// # back
//
// Get reference to last element in vector.
//
// # front
//
// Get reference to first element in vector.
//
// # at
//
// Like `[]`, but does bound checking and throws `out_of_range` in case of overflow.
{
std::vector<int> v{0, 1, 2};
assert(v.front() == 0);
assert(v.at(1) == 1);
assert(v.back() == 2);
try {
assert(v.at(3) == 0);
} catch (std::out_of_range& e) {
} catch (...) {
assert(false);
}
// Undefined on empty.
{
std::vector<int> v;
//v.front();
//v.back();
}
}
}
// # bool std::vector
//
// *bool std::vectors are evil!*
//
// The standard requires `vector` to have an specialization for bool which packs bits efficiently.
//
// While efficient, in order to work this specialization breaks common std::vector interfaces
// that require taking addresses only in the case of this specialization, since it does not
// make sense to takes addresses anymore.
//
// Alternatives to `vector<bool>`: <http://stackoverflow.com/questions/670308/alternative-to-vectorbool>
//
// A good alternative seem to be deque<bool>, which behaves as intended.
{
// Works fine and dandy as expected.
{
std::vector<int> v{1, 0};
int& i(v[0]);
}
// Does not compile!!!
{
std::vector<bool> v{true, false};
//bool& b(v[0]);
}
// It was not a problem with bool, the problem really is `vector<bool>`.
{
bool b[]{true, false};
bool& b2(b[0]);
}
}
}