Skip to content
Snippets Groups Projects
Commit a4f429bc authored by Michaël El Kharroubi's avatar Michaël El Kharroubi :satellite:
Browse files

cpp basics done

parent 06d7d0fc
Branches
No related tags found
No related merge requests found
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment