Ежедневный бит (е) C++ # 144, Представление n-арного преобразования C++ 23: std::views::zip_transform.
C++23 std::views::zip_transform — это представление, в котором i-й элемент является результатом применения n-арного преобразования, вызываемого к i-м элементам предоставленных диапазонов.
Представление представляет собой ленивую версию бинарного std::ranges::transform, обобщенную для любого количества диапазонов.
#include <ranges> #include <vector> std::vector<int> first{1,2,3,4}; std::vector<double> second{9.1, 9.2, 9.3, 9.4, 9.5}; std::vector<std::string> third{"a", "b", "c", "d", "e"}; // transform function takes one element from every range auto zip = [](int& a, double& b, std::string& c) { return a+b+(c[0]-'a'); }; // produce a view where the i-th element is the result of applying // the transformation function the i-th elements of the provided ranges // the length is determined by the shortest of the ranges for (auto v : std::views::zip_transform(zip, first,second,third)) { // iterate over: 10.1, 12.2, 14.3, 16.4 }
Откройте пример в Compiler Explorer.