format/formatranges2.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 <set>
#include <ranges>
#include <span>
#include "fibo3.hpp"  // coroutine for the first n Fibonacci numbers

int main()
{
  std::set names{"tic", "tac", "toe"};
  std::println("set:    {}", names);                          // set
  std::println("tuples: {}", names | std::views::enumerate);  // view of tuples

  int arr[] = {0, 8, 15, 47, 11, -1, 42};
  std::println("array:  {}", arr);                            // raw array

  std::span sp{arr};
  std::println("last:   {}", sp.last(3));                     // span

  std::generator<long> fibo = fibonacci(12);                  // coroutine interface
  std::println("fibo:   {}", fibo);
}