/**************************************************************** **** **** This file belongs with the course **** Introduction to Scientific Programming in C++/Fortran2003 **** copyright 2016-2023 Victor Eijkhout eijkhout@tacc.utexas.edu **** **** zip.cxx : C++23 range zipping **** **** Inspired by https://www.cppstories.com/2023/view-zip/ **** ****************************************************************/ #include using std::cout; #include using std::stringstream; #include using std::string; #include using std::vector; //#include // zip not yet in gcc12 #include #ifdef RANGES_V3_ALL_HPP namespace rng = ranges; #else namespace rng = std::ranges; #endif #include int main() { /* * old version */ { vector a { 10, 20, 30, 40, 50 }; vector b { "ten", "twenty", "thirty", "fourty" }; for (size_t i = 0; i < std::min(a.size(), b.size()); ++i) cout << fmt::format("{} -> {}\n", a[i], b[i]); // range version for (auto i : rng::iota_view{0ULL, std::min(a.size(), b.size())}) cout << fmt::format("{} -> {}\n", a[i], b[i]); for (size_t i = 0; i < std::min(a.size(), b.size()); ++i) { const auto& num = a[i]; const auto& name = b[i]; cout << fmt::format("{} -> {}\n", name, num); } } /* * Range zip version */ { cout << "Zip\n"; //codesnippet rangeszip vector a { 10, 20, 30, 40, 50 }; vector b { "one", "two", "three", "four" }; // zip in c++23, not yet in gcc12 for (const auto& [num, name] : rng::views::zip(a, b)) cout << fmt::format("{} -> {}\n", name, num); //codesnippet end cout << " .. zip\n"; } return 0; }