ranges/ziptransformview.cpp

The following code example is taken from the book
C++23 - The Complete Guide by Nicolai M. Josuttis, Leanpub, 2026
The code is licensed under a Creative Commons Attribution 4.0 International License. Creative Commons License

// raw code

#include <print>
#include <string>
#include <vector>
#include <list>
#include <ranges>
#include <chrono>
using namespace std::literals;

int main()
{
  std::vector v1 = {1, 2, 3, 4, 5};
  std::vector v2 = {4.4, 5.5, 6.6, 7.7};
  std::println("v1: {::<3}", v1);
  std::println("v2: {}", v2);

  for (auto val : std::views::zip_transform(std::multiplies{}, v1, v2)) {
    std::print("{:.2f} ", val);
  }
  std::println("");

  std::list coll = {5ms, 3ms, 20ms};
  std::println("\ncoll: {}", coll);

  auto mult3 = [&] (auto a, auto b, auto dist) {
                 return a * b * dist;
               };
  for (auto val : std::views::zip_transform(mult3, v1, v2, coll)) {
    std::print("{} ", val);
  }
  std::println("");
}