flat/flatmapalloc.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 <functional>
#include <numbers>
#include <flat_map>
#include "tracingpmr.hpp"

int main()
{
  // init a flat map with a tracing memory resource:
  TracingPMR tracingPool;
  std::flat_map<std::pmr::string, double, std::less<>,
                std::pmr::vector<std::pmr::string>,
                std::pmr::vector<double>>
    fm{&tracingPool};   
  
  std::println("*** insert pi:");
  fm.emplace("pi", std::numbers::pi);

  std::println("\n*** insert e:");
  fm.emplace("e", std::numbers::e);

  std::println("\n*** insert sqrt2:");
  fm.emplace("sqrt2", std::numbers::sqrt2);

  std::println("\n*** insert sqrt3:");
  fm.emplace("sqrt3", std::numbers::sqrt3);

  std::println("\n{}", fm);
}