7-Algorithms.ipynb 23.9 KB
Newer Older
1
2
3
4
5
6
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
7
    "# [Getting started in C++](./) - [Useful concepts and STL](./0-main.ipynb) - [Algorithms](./7-Algorithms.ipynb)"
8
9
10
11
12
13
14
15
16
17
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "toc": true
   },
   "source": [
    "<h1>Table of contents<span class=\"tocSkip\"></span></h1>\n",
    "<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#Introduction\" data-toc-modified-id=\"Introduction-1\">Introduction</a></span></li><li><span><a href=\"#Example:-std::sort\" data-toc-modified-id=\"Example:-std::sort-2\">Example: <code>std::sort</code></a></span></li><li><span><a href=\"#std::find\" data-toc-modified-id=\"std::find-3\"><code>std::find</code></a></span></li><li><span><a href=\"#Output-iterators-and-std::back_inserter\" data-toc-modified-id=\"Output-iterators-and-std::back_inserter-4\">Output iterators and <code>std::back_inserter</code></a></span></li><li><span><a href=\"#The-different-kinds-of-operators\" data-toc-modified-id=\"The-different-kinds-of-operators-5\">The different kinds of operators</a></span></li><li><span><a href=\"#Algorithm:-read-the-documentation-first!\" data-toc-modified-id=\"Algorithm:-read-the-documentation-first!-6\">Algorithm: read the documentation first!</a></span><ul class=\"toc-item\"><li><span><a href=\"#std::unique\" data-toc-modified-id=\"std::unique-6.1\">std::unique</a></span></li><li><span><a href=\"#std::remove\" data-toc-modified-id=\"std::remove-6.2\">std::remove</a></span></li></ul></li><li><span><a href=\"#Conclusion\" data-toc-modified-id=\"Conclusion-7\">Conclusion</a></span></li></ul></div>"
18
19
20
21
22
23
24
25
26
27
28
29
30
31
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Introduction\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
32
    "Even if C++ can't be qualified as a _batteries included_ language like Python (until C++ 17 there was no proper filesystem management, and the support of this feature was still shaky at best in several STL implementations one year ago...), there are plenty of algorithms that are already provided within the STL.\n",
33
    "\n",
34
    "We won't obviously list them all here - the mighty \\cite{Josuttis2012} which is more than 1000 pages long don't do it either! - but show few examples on how to use them. For instance, many STL algorithms rely upon iterators: this way a same algorithm may be used as well on `std::vector`, `std::list`, and so on...\n",
35
    "\n",
36
    "A side note: if a STL class provides a method which has a namesake algorithm, use the method. For instance there is a `std::sort` algorithm, but `std::list` provides a method which takes advantage on the underlying structure of the object and is therefore much more efficient.\n",
37
38
39
40
41
42
    "\n",
    "## Example: `std::sort`\n"
   ]
  },
  {
   "cell_type": "code",
43
   "execution_count": null,
44
   "metadata": {},
45
   "outputs": [],
46
47
48
49
50
   "source": [
    "#include <algorithm>\n",
    "#include <vector>\n",
    "#include <iostream>\n",
    "\n",
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
    "std::vector<int> int_vec { -9, 87, 11, 0, -21, 100 };\n",
    "std::sort(int_vec.begin(), int_vec.end());\n",
    "\n",
    "for (auto item : int_vec)\n",
    "    std::cout << item << \" \";"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <vector>\n",
    "\n",
    "std::deque<double> double_deque { -9., 87., 11., 0., -21., 100. };\n",
67
    "    \n",
68
    "std::sort(double_deque.begin(), double_deque.end(), std::greater<double>()); // optional third parameter is used  \n",
69
    "    \n",
70
71
    "for (auto item : double_deque)\n",
    "    std::cout << item << \" \";"
72
73
74
75
76
77
78
79
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As you can see, the same algorithm works upon two different types of objects. It works with non constant iterators; an optional third argument to `std::sort` enables to provide your own sorting algorithm.\n",
    "\n",
80
    "Lambda functions may be used as well to provide the comparison to use:"
81
82
83
84
   ]
  },
  {
   "cell_type": "code",
85
   "execution_count": null,
86
   "metadata": {},
87
   "outputs": [],
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
   "source": [
    "#include <algorithm>\n",
    "#include <vector>\n",
    "#include <iostream>\n",
    "\n",
    "{\n",
    "    std::vector<int> int_vec { -9, 87, 11, 0, -21, 100 };\n",
    "    \n",
    "    std::sort(int_vec.begin(), int_vec.end(), \n",
    "              [](auto lhs, auto rhs)\n",
    "              {\n",
    "                  const bool is_lhs_even = (lhs % 2 == 0);\n",
    "                  const bool is_rhs_even = (rhs % 2 == 0);                  \n",
    "\n",
    "                  // Even must be ordered first, then odds\n",
103
    "                  // Granted, this is really an oddball choice..\n",
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
    "                  if (is_lhs_even && !is_rhs_even)\n",
    "                      return true;\n",
    "                  \n",
    "                  if (is_rhs_even && !is_lhs_even)\n",
    "                      return false;\n",
    "                  \n",
    "                    return lhs < rhs;\n",
    "              });\n",
    "    \n",
    "    for (auto item : int_vec)\n",
    "        std::cout << item << \" \";\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Of course, we may use this on something other than `begin()` and `end()`; we just have to make sure iterators are valid:"
   ]
  },
  {
   "cell_type": "code",
127
   "execution_count": null,
128
   "metadata": {},
129
   "outputs": [],
130
131
132
133
134
135
136
137
138
139
140
141
   "source": [
    "#include <algorithm>\n",
    "#include <vector>\n",
    "#include <iostream>\n",
    "#include <cassert>\n",
    "\n",
    "{\n",
    "    std::vector<int> int_vec { -9, 87, 11, 0, -21, 100 };\n",
    "    \n",
    "    auto it = int_vec.begin() + 4;\n",
    "    assert(it < int_vec.end()); // Important condition to check iterator means something!\n",
    "    \n",
142
    "    std::sort(int_vec.begin(), it); // Only first four elements are sort.\n",
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
    "    \n",
    "    for (auto item : int_vec)\n",
    "        std::cout << item << \" \";\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `std::find`\n",
    "\n",
    "I will also show examples of `std::find` as it provides an additional common practice: it returns an iterator, and there is a specific behaviour if the algorithm failed to find something.\n"
   ]
  },
  {
   "cell_type": "code",
160
   "execution_count": null,
161
   "metadata": {},
162
   "outputs": [],
163
164
165
166
167
168
169
170
171
172
173
   "source": [
    "#include <vector>\n",
    "#include <algorithm>\n",
    "#include <iostream>\n",
    "\n",
    "{\n",
    "    std::vector<int> int_vec { -9, 87, 11, 0, -21, 100, -21 };\n",
    "    \n",
    "    const auto it = std::find(int_vec.cbegin(), int_vec.cend(), -21);\n",
    "    \n",
    "    if (it != int_vec.cend())\n",
174
    "        std::cout << \"Found at position \" << it - int_vec.cbegin() << std::endl;\n",
175
176
177
178
179
180
181
182
183
184
    "    else\n",
    "        std::cout << \"Not found.\" << std::endl;\n",
    "   \n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
185
    "As you can see, `std::find` returns the first instance in the iterator range (and you can also do arithmetic over the iterators). You may know how many instances there are with `std::count`:"
186
187
188
189
   ]
  },
  {
   "cell_type": "code",
190
   "execution_count": null,
191
   "metadata": {},
192
   "outputs": [],
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
   "source": [
    "#include <vector>\n",
    "#include <algorithm>\n",
    "#include <iostream>\n",
    "\n",
    "{\n",
    "    std::vector<int> int_vec { -9, 87, 11, 0, -21, 100, -21, 17, -21 };\n",
    "    \n",
    "    const auto count = std::count(int_vec.cbegin(), int_vec.cend(), -21);\n",
    "    \n",
    "    std::cout << \"There are \" << count << \" instances of -21.\" << std::endl;\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you want to use a condition rather than a value, there are dedicated versions of the algorithms to do so:"
   ]
  },
  {
   "cell_type": "code",
216
   "execution_count": null,
217
   "metadata": {},
218
   "outputs": [],
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
   "source": [
    "#include <vector>\n",
    "#include <algorithm>\n",
    "#include <iostream>\n",
    "\n",
    "{\n",
    "    std::vector<int> int_vec { -9, 87, 11, 0, -21, 100, -21, 17, -21 };\n",
    "    \n",
    "    const auto count = std::count_if(int_vec.cbegin(), int_vec.cend(),\n",
    "                                     [](int value)\n",
    "                                        {\n",
    "                                            return value % 2 == 0;\n",
    "                                        });\n",
    "    \n",
    "    std::cout << \"There are \" << count << \" even values in the list.\" << std::endl;\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Output iterators and `std::back_inserter`\n",
    "\n",
    "Some algorithms require output iterators: they don't work uniquely upon existing content but need to shove new data somewhere. You must in this case provide the adequate memory beforehand:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <vector>\n",
    "#include <algorithm>\n",
    "#include <iostream>\n",
    "\n",
    "{\n",
    "    std::vector<int> int_vec { -9, 87, 11, 0, -21, 100, -21, 17, -21 }; \n",
    "    std::vector<int> odd_only;\n",
    "    \n",
    "    std::copy_if(int_vec.cbegin(), int_vec.cend(),\n",
    "                 odd_only.begin(),\n",
    "                  [](int value)\n",
    "                  {\n",
    "                      return value % 2 != 0;\n",
    "                  }                                     \n",
    "                 ); // SHOULD MAKE YOUR KERNEL CRASH!\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
274
    "The issue is that the memory is not allocated first: the algorithm doesn't provide the memory at destination! (the reason is that an algorithm is as generic as possible; here `std::copy_if` is expected to work as well with `std::set`... and `std::vector` and `std::set` don't use the same API to allocate the memory).\n",
275
    "\n",
276
    "Of course, in some cases it is tricky to know in advance what you need, and here computing it previously with `std::count_if` add an additional operation. There is actually a way to tell the program to insert the values by `push_back` with `std::back_inserter`; it might be a good idea to reserve enough memory to use this method without recopy:"
277
278
279
280
   ]
  },
  {
   "cell_type": "code",
281
   "execution_count": null,
282
   "metadata": {},
283
   "outputs": [],
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
   "source": [
    "#include <vector>\n",
    "#include <algorithm>\n",
    "#include <iostream>\n",
    "\n",
    "{\n",
    "    std::vector<int> int_vec { -9, 87, 11, 0, -21, 100, -21, 17, -21 }; \n",
    "    std::vector<int> odd_only;\n",
    "    odd_only.reserve(int_vec.size()); // at most all elements of int_vec will be there\n",
    "    \n",
    "    std::copy_if(int_vec.cbegin(), int_vec.cend(),\n",
    "                 std::back_inserter(odd_only),\n",
    "                 [](int value)\n",
    "                 {\n",
    "                     return value % 2 != 0;\n",
    "                 }                                     \n",
    "                ); \n",
    "    \n",
302
    "    // And if you're afraid to have used too much memory with your `reserve()` call, \n",
303
304
    "    // you may call shrink_to_fit() method here.\n",
    "    \n",
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
    "    std::cout << \"The odd values are: \";\n",
    "    \n",
    "    for (auto item : odd_only)\n",
    "        std::cout << item << \" \";\n",
    "    \n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## The different kinds of operators\n",
    "\n",
    "`std::back_inserter` works only with containers that provide a `push_back()` method. This may be generalized: the fact that algorithms rely upon iterators to make them as generic as possible doesn't mean each algorithm will work on any container. \n",
    "\n",
    "There are actually several kinds of iterators:\n",
    "\n",
    "* **Forward iterators**, which which you may only iterate forward. For instance `std::forward_list` or `std::unordered_map` provide such iterators.\n",
    "* **Bidirectional iterators**, which way you may also iterate backward. For instance `std::list` or `std::map` provide such iterators.\n",
    "* **Random-access iterators**, which are bidirectional operators with on top of it the ability to provide random access (through an index). Think of `std::vector` or `std::string`.\n",
    "\n",
327
    "When you go on [cppreference](https://en.cppreference.com/w/) (or in \\cite{Josuttis2012}) the name of the template parameter explicitly describes which kind of iterator is actually used.\n",
328
    "\n",
329
    "Besides this classification, there are also in algorithms the difference between **input iterators** (which are read-only) and **output iterators** that assume you will write new content there."
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Algorithm: read the documentation first!\n",
    "\n",
    "You should really **carefully read the documentation** before using an algorithm: it might not behave as you believe...\n",
    "\n",
    "I will provide two examples:\n",
    "\n",
    "### std::unique\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
348
   "execution_count": null,
349
   "metadata": {},
350
   "outputs": [],
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
   "source": [
    "#include <vector>\n",
    "#include <algorithm>\n",
    "#include <iostream>\n",
    "\n",
    "{\n",
    "    std::vector<int> int_vec { -9, 87, 11, 0, -21, 100, -21, 17, -21 }; \n",
    "   \n",
    "    std::unique(int_vec.begin(), int_vec.end());\n",
    "       \n",
    "    std::cout << \"The unique values are (or not...): \";\n",
    "    \n",
    "    for (auto item : int_vec)\n",
    "        std::cout << item << \" \";\n",
    "    \n",
    "    std::cout << std::endl;\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So what's happen? If you look at [cppreference](http://www.cplusplus.com/reference/algorithm/unique/) you may see the headline is _Remove **consecutive** duplicates in range_.\n",
    "\n",
    "So to make it work you need to sort it first (or use a home-made algorithm if you need to preserve the original ordering):\n"
   ]
  },
  {
   "cell_type": "code",
381
   "execution_count": null,
382
   "metadata": {},
383
   "outputs": [],
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
   "source": [
    "#include <vector>\n",
    "#include <algorithm>\n",
    "#include <iostream>\n",
    "\n",
    "{\n",
    "    std::vector<int> int_vec { -9, 87, 11, 0, -21, 100, -21, 17, -21 }; \n",
    "   \n",
    "    std::sort(int_vec.begin(), int_vec.end());\n",
    "    std::unique(int_vec.begin(), int_vec.end());\n",
    "       \n",
    "    std::cout << \"The unique values are (really this time): \";\n",
    "    \n",
    "    for (auto item : int_vec)\n",
    "        std::cout << item << \" \";\n",
    "    \n",
    "    std::cout << std::endl;\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
408
    "Personally I have in my Utilities library a function `EliminateDuplicate()` which calls both in a row."
409
410
411
412
413
414
415
416
417
418
419
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### std::remove"
   ]
  },
  {
   "cell_type": "code",
420
   "execution_count": null,
421
   "metadata": {},
422
   "outputs": [],
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
   "source": [
    "#include <vector>\n",
    "#include <algorithm>\n",
    "#include <iostream>\n",
    "\n",
    "{\n",
    "    std::vector<int> int_vec { -9, 87, 11, 0, -21, 100, -21, 17, -21 }; \n",
    "   \n",
    "    std::remove_if(int_vec.begin(), int_vec.end(),\n",
    "                  [](int value)\n",
    "                   { \n",
    "                       return value % 2 != 0;\n",
    "                   });\n",
    "    \n",
    "    std::cout << \"The even values are (or not...): \";\n",
    "    \n",
    "    for (auto item : int_vec)\n",
    "        std::cout << item << \" \";\n",
    "    \n",
    "    std::cout << std::endl;\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So what happens this time? [cppreference](http://www.cplusplus.com/reference/algorithm/remove/?kw=remove) tells that it _transforms the range \\[first,last) into a range with all the elements that compare equal to val removed, and returns an iterator to the new end of that range_.\n",
    "\n",
    "In other words, `std::remove`:\n",
    "\n",
    "* Place at the beginning of the vector the values to be kept.\n",
    "* Returns an iterator to the **logical end** of the expected series...\n",
GILLES Sebastien's avatar
GILLES Sebastien committed
456
    "* But does not deallocate the memory! (and keeps the container's `size()` - see below)\n",
457
    "\n",
458
    "So to print the relevant values only, you should do:\n"
459
460
461
462
   ]
  },
  {
   "cell_type": "code",
463
   "execution_count": null,
464
   "metadata": {},
465
   "outputs": [],
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
   "source": [
    "#include <vector>\n",
    "#include <algorithm>\n",
    "#include <iostream>\n",
    "\n",
    "{\n",
    "    std::vector<int> int_vec { -9, 87, 11, 0, -21, 100, -21, 17, -21 }; \n",
    "   \n",
    "    auto logical_end = std::remove_if(int_vec.begin(), int_vec.end(),\n",
    "                                      [](int value)\n",
    "                                      { \n",
    "                                          return value % 2 != 0;\n",
    "                                      });\n",
    "    \n",
    "    std::cout << \"The even values are: \";\n",
    "    \n",
    "    for (auto it = int_vec.cbegin(); it != logical_end; ++it)\n",
    "        std::cout << *it << \" \";\n",
    "    \n",
    "    std::cout << std::endl;\n",
    "    std::cout << \"But the size of the vector is still \" << int_vec.size() << std::endl;\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And if you want to reduce this size, you should use the `std::vector::erase()` method:"
   ]
  },
  {
   "cell_type": "code",
499
   "execution_count": null,
500
   "metadata": {},
501
   "outputs": [],
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
   "source": [
    "#include <vector>\n",
    "#include <algorithm>\n",
    "#include <iostream>\n",
    "\n",
    "{\n",
    "    std::vector<int> int_vec { -9, 87, 11, 0, -21, 100, -21, 17, -21 }; \n",
    "   \n",
    "    auto logical_end = std::remove_if(int_vec.begin(), int_vec.end(),\n",
    "                                      [](int value)\n",
    "                                      { \n",
    "                                          return value % 2 != 0;\n",
    "                                      });\n",
    "    \n",
    "    int_vec.erase(logical_end, int_vec.end());\n",
    "    \n",
    "    std::cout << \"The even values are: \";\n",
    "    \n",
    "    for (auto item : int_vec)\n",
    "        std::cout << item << \" \";\n",
    "    \n",
    "    std::cout << std::endl;\n",
    "    std::cout << \"And the size of the vector is correctly \" << int_vec.size() << std::endl;\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Conclusion\n",
    "\n",
    "My point was absolutely not to tell you not to use the STL algorithms; on the contrary it is better not to reinvent the wheel, especially considering you would likely end up with a less efficient version of the algorithm!\n",
    "\n",
536
537
538
    "You need however to be very careful: sometimes the names are unfortunately misleading, and you should always check a function does the job you have in mind. Algorithms were written to be as generic as possible, and can't do some operations such as allocate or deallocate memory as it would break this genericity.\n",
    "\n",
    "I have barely scratched the surface; many algorithms are extremely useful. So whenever you want to proceed with a transformation that is likely common (check a range is sorted, partition a list in a specific way, finding minimum and maximum, etc...) it is highly likely the STL has something in store for you.\n",
539
    "\n",
540
541
    "The reading of \\cite{Cukic2018} should provide more incentive to use them.\n",
    "\n",
GILLES Sebastien's avatar
GILLES Sebastien committed
542
    "It is also important to highlight that while the STL algorithms may provide you efficiency (this library is written by highly skilled engineers after all), this is not its main draw: the algorithms are written to be as generic as possible. The primary reason to use them is to allow you to think at a higher level of abstraction, not to get the fastest possible implementation. So if your ~~intuition~~ benchmarking has shown that the standard library is causing a critical slowdown, you are free to explore classic alternatives such as loop unrolling - that's one of the strength of the language (and the STL itself opens up this possibility directly for some of its construct - you may for instance use your own memory allocator when defining a container). For most purposes however that will not be necessary.\n",
543
    "\n",
544
    "FYI, C++ 20 introduce a completely new way to deal with algorithms, which does not rely on direct use of iterators but instead on a range library. This leads to a syntax which is more akin to what is done in other languages - see for instance this example lifted from this [blog post](https://www.modernescpp.com/index.php/c-20-the-ranges-library):\n"
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
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// C++ 20: does not run in Xeus-cling!\n",
    "\n",
    "#include <iostream>\n",
    "#include <ranges>\n",
    "#include <vector>\n",
    "\n",
    "int main(int argc, char** argv) \n",
    "{\n",
    "\n",
    "    std::vector<int> numbers = {1, 2, 3, 4, 5, 6};\n",
    "  \n",
    "    auto results = numbers | std::views::filter([](int n){ return n % 2 == 0; })\n",
    "                           | std::views::transform([](int n){ return n * 2; });\n",
    "                           \n",
    "    for (auto v: results) std::cout << v << \" \";     // 4 8 12\n",
    "\n",
    "    return EXIT_SUCCESS;\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Having no first hand experience of it I really can't say more about it but don't be astonished if you meet such a syntax in a C++ program."
578
579
   ]
  },
580
581
582
583
584
585
586
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# References\n",
    "\n",
    "[<a id=\"cit-Josuttis2012\" href=\"#call-Josuttis2012\">Josuttis2012</a>] Nicolai M. Josuttis, ``_The C++ Standard Library: A Tutorial and Reference_'',  2012.\n",
587
588
    "\n",
    "[<a id=\"cit-Cukic2018\" href=\"#call-Cukic2018\">Cukic2018</a>] Ivan Čukić, ``_Functional Programming in C++_'', 01 2018.\n",
589
590
591
    "\n"
   ]
  },
592
593
594
595
596
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
597
    "© _CNRS 2016_ - _Inria 2018-2021_   \n",
598
599
    "_This notebook is an adaptation of a lecture prepared by David Chamont (CNRS) under the terms of the licence [Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)](http://creativecommons.org/licenses/by-nc-sa/4.0/)_  \n",
    "_The present version has been written by Sébastien Gilles and Vincent Rouvreau (Inria)_"
600
601
602
603
604
605
606
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "C++17",
   "language": "C++17",
GILLES Sebastien's avatar
GILLES Sebastien committed
607
   "name": "xcpp17"
608
609
610
611
612
613
  },
  "language_info": {
   "codemirror_mode": "text/x-c++src",
   "file_extension": ".cpp",
   "mimetype": "text/x-c++src",
   "name": "c++",
GILLES Sebastien's avatar
GILLES Sebastien committed
614
   "version": "17"
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
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "key",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": true,
   "title_cell": "Table of contents",
   "title_sidebar": "Contents",
   "toc_cell": true,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
646
647
648
  }
 },
 "nbformat": 4,
649
 "nbformat_minor": 4
650
}