From a4f429bc15fe3e26308412368b753652ad086bd6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?El=20Kharroubi=20Micha=C3=ABl?=
 <michael.el-kharroubi@hesge.ch>
Date: Mon, 26 Feb 2024 15:16:58 +0100
Subject: [PATCH] cpp basics done

---
 cpp_basics_for_STL.typ | 56 ++++++++++++++++++++++++++++++++++--------
 1 file changed, 46 insertions(+), 10 deletions(-)

diff --git a/cpp_basics_for_STL.typ b/cpp_basics_for_STL.typ
index 1594aeb..6d816a2 100644
--- a/cpp_basics_for_STL.typ
+++ b/cpp_basics_for_STL.typ
@@ -342,8 +342,7 @@ std::cout << is_sorted << std::endl;
 std::vector<int> v = {6, 3, 4, 12, 1, 15};
 std::vector<std::string> parity{6};
 
-std::transform(v.begin(),
-               v.end(),
+std::transform(v.begin(), v.end(),
                parity.begin(),
                [](int i){
                   std::string s = (i % 2) == 0 ? "even" : "odd";
@@ -360,21 +359,58 @@ std::cout << std::endl;
 #slide(title: "Exemple de reduction")[
 ```cpp
 constexpr int kN = 100;
-std::vector<int> v{kN, 0};
+std::vector<int> v(kN, 0);
 
 std::iota(v.begin(), v.end(), 1);
 
-int sum = std::accumulate(v.begin(),
-                v.end(),
-                0,
-                [](int acc, int i){
-                  return acc + i;
-                });
+int sum = std::accumulate(v.begin(), v.end(),
+                          0,
+                          [](int acc, int i){
+                            return acc + i;
+                          });
 
-std::cout << sum << std::endl;
+bool sum_correct = sum == (kN*(kN+1))/2;
+std::cout << sum_correct << std::endl;
+```
+]
+
+#slide(title: "Exemple de suppression conditionnelle (filter)")[
+```cpp
+std::vector<int> v = {6, 3, 4, 12, 1, 15};
+
+auto last = std::remove_if(v.begin(), v.end(),
+                           [](int i){
+                               return  (i % 2) == 1;
+                           });
+
+size_t filtered_size = std::distance(v.begin(), last);
+v.resize(filtered_size);
+
+for (int i : v){
+  std::cout << i << " ";
+}
+std::cout << std::endl;
 ```
 ]
 
 #new-section-slide("Notions à retenir")
 
+#slide(
+  title: "Notions essentielles à retenir",
+)[
+- Les collections proposent un itérateur sur le début et la fin avec les méthodes
+  `begin` et `end`
+- Un `std::vector` est un tableau de taille dynamique
+- Un `std::array` est un tableau statique
+- Si l'on est pas responsable des données, il faut utiliser un `std::span`
+- Les lambdas sont des fonctions anonymes qui peuvent capturer totalement ou
+  partiellement leur environnement
+- On peut capturer par référence ou par copie
+- Les algorithmes STL à connaître sont :
+  - `iota`
+  - `transform`
+  - `accumulate`
+  - `remove_if`
+]
+
 #new-section-slide("Questions ?")
\ No newline at end of file
-- 
GitLab