format/formatnested.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 <vector>
#include <list>
#include <ranges>

int main()
{
  // print nested collections:
  std::vector<std::list<int>> coll2d{{1, 1, 1},
                                     {47, 11},
                                     {0, 8, 15}};
  std::println("coll2d:  {}", coll2d);

  // print sub-views of three elements:
  std::vector<double> coll{0, 8, 15, 47, 11, -1, 42};
  std::println("chunked: {}", coll | std::views::chunk(3));
}