lundi 17 octobre 2022

C++: Nested dictionaries with unordered_maps

I'm trying to write some code that will allow me to create a dictionary with the unordered_map object in C++. It will basically look like

string1
    string2
        int_vec1
    string3
        int_vec2
...

i.e. It's a dictionary of string and integer-vector pairs, indexed by strings.

I have the following code of a simplified example to illustrate:

#include <chrono>
#include <iostream>
#include <vector>
#include <map>
#include <fstream>
#include <ctime>
#include <string>
#include <unordered_map>

int main(int argc, char **argv) {
  std::string key_0 = "key_0";
  std::string key_01 = "key_01";
  std::string key_02 = "key_02";
  std::string key_1 = "key_1";
  std::string key_11 = "key_11";
  std::string key_12 = "key_12";
  std::string key_13 = "key_13";
  std::vector<int> val_01 = {1,2,3,4};
  std::vector<int> val_02 = {1,2,3,4};
  std::vector<int> val_11 = {1,2,3,4};
  std::vector<int> val_12 = {1,2,3,4};
  std::vector<int> val_13 = {1,2,3,4};

  std::unordered_map<std::string, std::unordered_map<std::string, std::vector<int>>> my_dict;
  my_dict.insert({key_0, std::pair<std::string, std::vector<int>>(key_01, val_01)});

}

However, when I compile this using gcc version 11.2.0, I get the following error

test_make_nested_unordered_map.cpp:25:17: error: no matching function for call to ‘std::unordered_map<std::__cxx11::basic_string<char>, std::unordered_map<std::__cxx11::basic_string<char>, std::vector<int> > >::insert(<brace-enclosed initializer list>)’
   25 |   my_dict.insert({key_0, std::pair<std::string, std::vector<int>>(key_01, val_01)});
      |   ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The code seems fine to me. But I don't know why it isn't working. I would greatly appreciate some help with this. My actual code is more complicated, but this is just meant to be a simplified reproducible example.

Thanks for the help

Aucun commentaire:

Enregistrer un commentaire