3

I have a nested map, i.e., map<int, map<int, string>> that I'd like to initialize with an initializer list. I can use an initializer list to initialize a single-level map, but can't seem to figure out the proper syntax for a nested map. Is it even possible?

MWE:

// This example shows how to initialize some maps
// Compile with this command:
//      clang++ -std=c++11 -stdlib=libc++ map_initialization.cpp -o map_initialization

#include <iostream>
#include <map>
#include <string>

using namespace std;

int main(){
    cout << "\nLearning map initialization.\n" << endl;

    map<int, string> level1map = {
        {1, "a"},
        {2, "b"},
        {3, "c"}
    };

    for (auto& key_value : level1map) {
        cout << "key: " << key_value.first << ", value=" << key_value.second << endl;
    }

//  This section doesn't compile
//  map<int, map<int, string>> level2map = {
//      {0,
//          {0, "zero"},
//          {1, "one"},
//          {2, "two"}
//      },

//      {1,
//          {0, "ZERO"},
//          {1, "ONE"},
//          {2, "TWO"}
//      }
//  };

    return 0;
}

4 Answers 4

11

You're just missing a pair of braces around the inner map contents:

map<int, map<int, string>> level2map = {
    {0, {
        {0, "zero"},
        {1, "one"},
        {2, "two"}
    }},

    {1, {
        {0, "ZERO"},
        {1, "ONE"},
        {2, "TWO"}
    }}
};

Perhaps it would be more noticeable if you wrote it out in one line. A list of four things:

{0, {0, "zero"}, {1, "one"}, {2, "two"}}

vs. a list of 2 things, where the 2nd thing is a list of 3 things:

{0, {{0, "zero"}, {1, "one"}, {2, "two"}}}
Sign up to request clarification or add additional context in comments.

Comments

0
map<int, map<int, string>> level2map = {
        { 0,
        { { 0, "zero" },
        { 1, "one" },
        { 2, "two" } }
        },

        { 1,
        { { 0, "ZERO" },
        { 1, "ONE" },
        { 2, "TWO" } }
        }
};

Comments

0

You are just missing {} for second value of first pair:

map<int, map<int, string>> level2map = {
  {0, {
          {0, "zero"},
          {1, "one"},
          {2, "two"}
      }
  },

  {1, {
          {0, "ZERO"},
          {1, "ONE"},
          {2, "TWO"}
      }
  }
};

Comments

0

Map of map Initialization:

map<int, map<int, string>> level2map= {{0, {{0, "zero"},{1, "one"},{2, "two"}}},
                                       {1, {{0, "ZERO"},{1, "ONE"},{2, "TWO"}}}};

Additionally, if you want to access all the elements from the map of map then you could use the below method.

for (auto const& x : level2map) {
    cout << x.first << ":";      //
    for (auto const& y : x.second) {
        cout << " " << y.first << " "<< y.second; 
    }
    cout << endl;
}

output:

0: 0 zero 1 one 2 two
1: 0 ZERO 1 ONE 2 TWO

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.