//********************************************************
// The following code example is taken from the book
//  C++23 - The Complete Guide
//  by Nicolai M. Josuttis (www.josuttis.com)
//  http://www.cppstd23.com
//
// The code is licensed under a
//  Creative Commons Attribution 4.0 International License
//  http://creativecommons.org/licenses/by/4.0/
//********************************************************


#include <print>
#include <vector>
#include <functional>
#include <ranges>
#include <chrono>

int main()
{
  std::vector<int> coll{47, 11, 0, 8, 15, -11, 7};
  std::println("coll: {}", coll);

  auto isOdd = [] (auto val) { return val % 2 != 0; };
  auto vecView = coll | std::views::filter(isOdd);
  for (int& elem : vecView) {
    elem = elem * elem;
  }
  std::println("   => {}", coll);

  coll = {47, 11, 0, 8, 15, -11, 7};
  std::println("\ncoll: {}", coll);

  auto vec1 = coll | std::views::filter(isOdd) | std::ranges::to<std::vector>();
  for (int& elem : vec1) {
    elem = elem * elem;
  }
  std::println("   => {}", coll);
  std::println("vec1: {}", vec1);

  coll = {47, 11, 0, 8, 15, -11, 7};
  std::println("\ncoll: {}", coll);

  auto vec2 = coll | std::views::filter(isOdd)
                   | std::ranges::to<std::vector<std::reference_wrapper<int>>>();
  for (int& elem : vec2) {
    elem = elem * elem;
  }
  std::println("   => {}", coll);
}

