flat/flatseterase.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 <flat_set>

int main()
{
  std::flat_set coll{2, 3, 5, 8, 11, 13, 20, 33, 99};  // flat set without duplicates
  std::println("coll: {}", coll);

  auto pos5 = coll.lower_bound(5);              // from elements greater or equal to 5
  auto pos20 = coll.upper_bound(20);            // to elements less or equal to 20

  for (auto pos = pos5; pos != pos20; ++pos) {  // print this subset of elements
    std::print("{} ", *pos);
  }
  std::println("");

  coll.erase(11);                               // remove element with value 11
  coll.erase(13);                               // remove element with value 13

  for (auto pos = pos5; pos != pos20; ++pos) {  // FATAL RUNTIME ERROR
    std::print("{} ", *pos);
  }
}