C++ recipes

Operating over a whole container using ranges (C++20) #include <vector>#include <algorithm>#include <random>#include <iostream> int main() { const auto printy = [](const auto &vec){ for (size_t i = 0; const auto&v : vec) std::cout << v << (++i == vec.size() ? "\n" : " "); }; std::vector<int> v(4); printy(v); std::ranges::fill(v, -1); printy(v); std::ranges::generate(v, std::rand); printy(v); } Generates: 0 0 0 0 -1 -1 -1 -1 1804289383 846930886 1681692777 1714636915 https://godbolt.org/z/31196Kz5z Zip two containers #include <iostream>#include <vector>#include <algorithm>#include <ranges> int main() { const std::vector<int> a{1, 2, 3}; const std::vector<int> b{4, 5, 6}; std::vector<std::pair<int, int>> zipped; zipped. [Read More]