8.2.3 : Le main.cpp
Voici le main.cpp : Commençons par les includes standards :
1 2 |
#include <iostream> #include <vector> |
Ensuite, les includes qui permettent d'utiliser zip, iota et repeat :
1 2 3 4 5 |
#include <ranges> #include <numeric> #include <execution> #include <algorithm> |
Définissions notre fonction principale :
1 |
int main(){ |
Déclarons quelques vecteurs :
1 2 |
size_t nbValue(10lu); std::vector<float> vecA(nbValue), vecB(nbValue), vecC(nbValue); |
Initialisons les vecteurs :
1 2 3 |
std::iota(vecA.begin(), vecA.end(), 0); std::iota(vecB.begin(), vecB.end(), 10); std::iota(vecC.begin(), vecC.end(), 100); |
Puis, utilisons zip afin de les parcourir en même temps :
1 2 3 4 |
std::cout << "Test with simple zip :" << std::endl; for (const auto & [a, b, c] : std::views::zip(vecA, vecB, vecC)) { std::cout << "a = " << a << ", b = " << b << ", c = " << c << std::endl; } |
Combinons zip et repeat :
1 2 3 4 5 6 7 8 9 10 11 12 |
std::cout << "Test with zip of repeat :" << std::endl; size_t nbRepeat(2lu); for (const auto & [a, b, c] : std::views::zip( std::views::repeat(vecA, nbRepeat), std::views::repeat(vecB, nbRepeat), std::views::repeat(vecC, nbRepeat))) { //For some reason, the repeat gives vector of vector of elements and not only vector of elements, so we can call .front() std::cout << "a.front() = " << a.front() << ", b.front() = " << b.front() << ", c.front() = " << c.front() << std::endl; } return 0; } |
Le fichier main.cpp complet :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include <iostream> #include <vector> #include <ranges> #include <numeric> #include <execution> #include <algorithm> int main(){ size_t nbValue(10lu); std::vector<float> vecA(nbValue), vecB(nbValue), vecC(nbValue); std::iota(vecA.begin(), vecA.end(), 0); std::iota(vecB.begin(), vecB.end(), 10); std::iota(vecC.begin(), vecC.end(), 100); std::cout << "Test with simple zip :" << std::endl; for (const auto & [a, b, c] : std::views::zip(vecA, vecB, vecC)) { std::cout << "a = " << a << ", b = " << b << ", c = " << c << std::endl; } std::cout << "Test with zip of repeat :" << std::endl; size_t nbRepeat(2lu); for (const auto & [a, b, c] : std::views::zip( std::views::repeat(vecA, nbRepeat), std::views::repeat(vecB, nbRepeat), std::views::repeat(vecC, nbRepeat))) { //For some reason, the repeat gives vector of vector of elements and not only vector of elements, so we can call .front() std::cout << "a.front() = " << a.front() << ", b.front() = " << b.front() << ", c.front() = " << c.front() << std::endl; } return 0; } |
Le fichier main.cpp est disponible ici.