Skip to content

Commit 8acab1e

Browse files
Update C++ Collections
1 parent 3d7b51d commit 8acab1e

1 file changed

Lines changed: 64 additions & 38 deletions

File tree

  • docs/programming-languages/collections

docs/programming-languages/collections/_cpp.mdx

Lines changed: 64 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,14 @@ std::int32_t howManyOdd = std::count_if(std::begin(my_collection), std::end(my_c
6262
// ------------------------------------
6363
// Filter
6464
// ------------------------------------
65-
// TODO
65+
std::vector<std::int32_t> filtered;
66+
std::copy_if(std::begin(myArray), std::end(myArray), std::back_inserter(filtered), [](auto element) { return element > 10; });
6667

6768
// ------------------------------------
6869
// Find (return Index)
6970
// ------------------------------------
70-
// TODO
71+
auto iterator = std::find(std::begin(myArray), std::end(myArray), target);
72+
auto index = iterator == std::end(myArray) ? -1 : std::distance(std::begin(myArray), iterator);
7173

7274
// ------------------------------------
7375
// Accumulate (sum all values)
@@ -119,7 +121,7 @@ std::copy(std::begin(myArray), std::end(myArray), std::begin(myCopyArray));
119121
// ------------------------------------
120122
// Slice
121123
// ------------------------------------
122-
// TODO
124+
std::vector<std::int32_t> slice(std::begin(myArray) + 1, std::begin(myArray) + 3);
123125

124126
// ------------------------------------
125127
// Printing Members
@@ -213,42 +215,49 @@ myList.max_size();
213215
// ------------------------------------
214216
// Count (How many of)
215217
// ------------------------------------
216-
// TODO
218+
auto howMany = std::count(myList.begin(), myList.end(), target);
219+
auto howManyOdd = std::count_if(myList.begin(), myList.end(), [](auto element) { return element % 2 != 0; });
217220

218221
// ------------------------------------
219222
// Filter
220223
// ------------------------------------
221-
// TODO
224+
std::list<std::int32_t> filtered;
225+
std::copy_if(myList.begin(), myList.end(), std::back_inserter(filtered), [](auto element) { return element > 10; });
222226

223227
// ------------------------------------
224228
// Find (return Index)
225229
// ------------------------------------
226-
// TODO
230+
auto iterator = std::find(myList.begin(), myList.end(), target);
231+
auto index = iterator == myList.end() ? -1 : std::distance(myList.begin(), iterator);
227232

228233
// ------------------------------------
229234
// Accumulate (sum all values)
230235
// ------------------------------------
231-
// TODO
236+
auto total = std::accumulate(myList.begin(), myList.end(), 0);
232237

233238
// ------------------------------------
234239
// Sort
235240
// ------------------------------------
236-
// TODO
241+
myList.sort();
242+
myList.sort([](auto firstElement, auto secondElement) { return firstElement > secondElement; });
237243

238244
// ------------------------------------
239245
// Comparing
240246
// ------------------------------------
241-
// TODO
247+
bool isEqual = myList == myOtherList;
242248

243249
// ------------------------------------
244250
// Copy / Clone
245251
// ------------------------------------
246-
// TODO
252+
auto myCopyList = myList;
247253

248254
// ------------------------------------
249255
// Slice
250256
// ------------------------------------
251-
// TODO
257+
std::list<std::int32_t> slice;
258+
auto first = std::next(myList.begin(), 1);
259+
auto last = std::next(myList.begin(), 3);
260+
std::copy(first, last, std::back_inserter(slice));
252261

253262
// ------------------------------------
254263
// Printing Members
@@ -317,10 +326,10 @@ myVector.at(0) = 14; // with bounds-checking
317326
// ------------------------------------
318327
// Push & Pop
319328
// ------------------------------------
320-
myVector.push_front(30); // Add at the beginning
329+
myVector.insert(myVector.begin(), 30); // Add at the beginning
321330
myVector.push_back(30); // Add at the end
322-
myVector.pop_front(); // Removes the first element of the list, and reduces size of the list by 1.
323-
myVector.pop_back(); // Removes the last element of the list, and reduces size of the list by 1.
331+
myVector.erase(myVector.begin()); // Removes the first element of the vector, and reduces size of the vector by 1.
332+
myVector.pop_back(); // Removes the last element of the vector, and reduces size of the vector by 1.
324333

325334
// ------------------------------------
326335
// Size / Length
@@ -331,42 +340,46 @@ myVector.max_size();
331340
// ------------------------------------
332341
// Count (How many of)
333342
// ------------------------------------
334-
// TODO
343+
auto howMany = std::count(myVector.begin(), myVector.end(), target);
344+
auto howManyOdd = std::count_if(myVector.begin(), myVector.end(), [](auto element) { return element % 2 != 0; });
335345

336346
// ------------------------------------
337347
// Filter
338348
// ------------------------------------
339-
// TODO
349+
std::vector<std::int32_t> filtered;
350+
std::copy_if(myVector.begin(), myVector.end(), std::back_inserter(filtered), [](auto element) { return element > 10; });
340351

341352
// ------------------------------------
342353
// Find (return Index)
343354
// ------------------------------------
344-
// TODO
355+
auto iterator = std::find(myVector.begin(), myVector.end(), target);
356+
auto index = iterator == myVector.end() ? -1 : std::distance(myVector.begin(), iterator);
345357

346358
// ------------------------------------
347359
// Accumulate (sum all values)
348360
// ------------------------------------
349-
// TODO
361+
auto total = std::accumulate(myVector.begin(), myVector.end(), 0);
350362

351363
// ------------------------------------
352364
// Sort
353365
// ------------------------------------
354-
// TODO
366+
std::sort(myVector.begin(), myVector.end());
367+
std::sort(myVector.begin(), myVector.end(), [](auto firstElement, auto secondElement) { return firstElement > secondElement; });
355368

356369
// ------------------------------------
357370
// Comparing
358371
// ------------------------------------
359-
// TODO
372+
bool isEqual = myVector == myOtherVector;
360373

361374
// ------------------------------------
362375
// Copy / Clone
363376
// ------------------------------------
364-
// TODO
377+
auto myCopyVector = myVector;
365378

366379
// ------------------------------------
367380
// Slice
368381
// ------------------------------------
369-
// TODO
382+
std::vector<std::int32_t> slice(myVector.begin() + 1, myVector.begin() + 3);
370383

371384
// ------------------------------------
372385
// Printing Members
@@ -409,8 +422,9 @@ void my_function(std::vector<std::int32_t> const &myVector)
409422
std::vector<std::vector<std::int32_t> > my_multi_vector;
410423
std::vector<std::vector<std::int32_t> > my_multi_vector {{{1,2,3},{4,5,6},{7,8,9}}};
411424

412-
// Legacy (Avoid)
413-
// TODO: Examples
425+
// Alternative modern syntax
426+
std::vector<std::vector<std::int32_t>> my_multi_vector;
427+
std::vector<std::vector<std::int32_t>> my_multi_vector {{1,2,3},{4,5,6},{7,8,9}};
414428
```
415429

416430
```Cpp
@@ -423,25 +437,28 @@ std::vector<std::vector<std::int32_t> > my_multi_vector {{{1,2,3},{4,5,6},{7,8,9
423437
// ------------------------------------
424438

425439
// Uniform Initialization (since C++11)
426-
// TODO
440+
std::tuple<std::string, std::int32_t, bool> myTuple { "hello", 10, true };
427441

428442
// Const Declaration
429-
// TODO
443+
const std::tuple<std::string, std::int32_t, bool> myTuple { "hello", 10, true };
444+
std::tuple<std::string, std::int32_t, bool> const myTuple { "hello", 10, true };
430445

431446
// ------------------------------------
432447
// Direct Access
433448
// ------------------------------------
434-
// TODO
449+
auto text = std::get<0>(myTuple);
450+
auto number = std::get<1>(myTuple);
435451

436452
// ------------------------------------
437453
// Push & Pop
438454
// ------------------------------------
439-
// TODO
455+
// Not Available
456+
// Tuples have fixed size and fixed element positions.
440457

441458
// ------------------------------------
442459
// Size / Length
443460
// ------------------------------------
444-
// TODO
461+
auto tupleSize = std::tuple_size<decltype(myTuple)>::value;
445462

446463
// ------------------------------------
447464
// Count (How many of)
@@ -450,47 +467,56 @@ std::vector<std::vector<std::int32_t> > my_multi_vector {{{1,2,3},{4,5,6},{7,8,9
450467
// ------------------------------------
451468
// Filter
452469
// ------------------------------------
453-
// TODO
470+
// Not Available
471+
// Tuples are fixed heterogeneous values, not runtime-filterable collections.
454472

455473
// ------------------------------------
456474
// Find (return Index)
457475
// ------------------------------------
458-
// TODO
476+
// Not Available
477+
// Access tuple members by known compile-time index or structured binding.
459478

460479
// ------------------------------------
461480
// Accumulate (sum all values)
462481
// ------------------------------------
463-
// TODO
482+
// Not Available for general heterogeneous tuples.
483+
// Use std::apply with custom logic when the tuple values are compatible.
464484

465485
// ------------------------------------
466486
// Sort
467487
// ------------------------------------
468-
// TODO
488+
// Not Available
489+
// Tuples represent fixed positions, not sortable collections.
469490

470491
// ------------------------------------
471492
// Comparing
472493
// ------------------------------------
473-
// TODO
494+
bool isEqual = myTuple == myOtherTuple;
474495

475496
// ------------------------------------
476497
// Copy / Clone
477498
// ------------------------------------
478-
// TODO
499+
auto myCopyTuple = myTuple;
479500

480501
// ------------------------------------
481502
// Slice
482503
// ------------------------------------
483-
// TODO
504+
// Not Available
505+
// Use a new tuple with the wanted fields.
506+
auto slice = std::make_tuple(std::get<0>(myTuple), std::get<1>(myTuple));
484507

485508
// ------------------------------------
486509
// Printing Members
487510
// ------------------------------------
488-
// TODO
511+
std::cout << std::get<0>(myTuple) << " " << std::get<1>(myTuple) << " " << std::get<2>(myTuple);
489512

490513
// ------------------------------------
491514
// As a function Parameter
492515
// ------------------------------------
493-
// TODO
516+
void my_function(const std::tuple<std::string, std::int32_t, bool> &myTuple)
517+
{
518+
// non-mutable code
519+
}
494520
```
495521
496522
> More Info:

0 commit comments

Comments
 (0)