ranges/cachemod.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 <ranges>

int main()
{
  std::vector<int> coll{1, 2, 3, 4, 1, 2, 3, 4};
  std::println("coll:        {}", v);

  // define and USE view:
  auto v = coll | std::views::split(1);
  std::println("split(3):    {}\n", v);

  // modify underlying range:
  coll.erase(coll.begin());

  // use view again:
  std::println("1st removed: {}", coll);
  std::println("split again: {}", v);
}