diff --git a/cpp_basics_for_STL.typ b/cpp_basics_for_STL.typ index 1594aeba23432c0e18f272def3108c8be3b84a51..6d816a2fe9d60f0ccf2d7414f063ee76f6405082 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