flat/flatmapconts.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 <string>
#include <vector>
#include <deque>
#include <flat_map>

int main()
{
  // define two associated containers:
  std::deque<std::string> last{"Turner", "Bond", "Holmes"};
  std::vector<std::string> first{"Tina", "James", "Sherlock"};
  std::println("last:  {}", last);
  std::println("first: {}", first);

  // initialize a flat map from two associated containers:
  std::flat_map fm1{last, first};                                  // OK
  std::println("fm1:   {}", fm1);

  std::flat_map<std::string, std::string, std::less<std::string>,
                decltype(last), decltype(first)> fm2{last, first}; // OK
  std::println("fm2:   {}", fm2);

  // declare a flat map for two associated containers:
  std::flat_map<std::string, std::string, std::less<std::string>,
                std::deque<std::string>, std::vector<std::string>> fm3;
  //...
  // assign values from two containers:
  fm3 = decltype(fm3){last, first};                                // OK
  std::println("fm3:   {}", fm3);
}