jlconlin
jlconlin

Reputation: 15054

C++11 Nested Map with List-Initialization

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;
}

Upvotes: 2

Views: 4763

Answers (4)

SridharKritha
SridharKritha

Reputation: 9611

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

Upvotes: 0

Slava
Slava

Reputation: 44238

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"}
      }
  }
};

Upvotes: 0

Joseph Mansfield
Joseph Mansfield

Reputation: 110648

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"}}}

Upvotes: 10

Neil Kirk
Neil Kirk

Reputation: 21763

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

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

Upvotes: 0

Related Questions