Back to TILs

C++ Ranges 01


  // loop detect lenght
  int ints[] = {0, 1, 2, 3, 4, 5};
  for (const auto i : ints) {
    std::cout << i << ' ';
  }
  std::cout << '\n';

  // (4,10] = 4 5 6 7 8 9
  for (int i : std::ranges::iota_view{4, 10}) {
    std::cout << i << ' ';
  }
  std::cout << '\n';

  // (5,10] = 5 6 7 8 9
  for (int i : std::views::iota(5, 10)) {
    std::cout << i << ' ';
  }
  std::cout << '\n';

  std::vector<double> vec{ 1.1, 2.2, 4.3, 5.6, 2.4 };
  auto                squared = vec | std::ranges::views::transform( []( auto const i ) { return i * i; } );
  // squared                     = vec | std::range::views::transform( std::sqrt );
  for( const auto &v : squared ) {
    std::cout << v << '\n';
  }
  std::cout << '\n';

Output

0 1 2 3 4 5 
4 5 6 7 8 9 
5 6 7 8 9 
1.21
4.84
18.49
31.36
5.76