pankaj kushwaha
pankaj kushwaha

Reputation: 379

unable to insert data in a stl map of map of map

I have following map declaration :

std::map<std::string, std::map<std::string, std::map<std::string, std::string>>> m;

I tried inserting data in following way :

m.insert({ "HARDWARE\\DESCRIPTION\\System\\BIOS", { "REG_SZ", { "SystemSKU", "SystemSKU" } } });

but its not working , what is correct syntax to insert data?

Upvotes: 1

Views: 67

Answers (2)

Saurav Sahu
Saurav Sahu

Reputation: 13934

This list initialization will work for you:

std::map<std::string, std::map<std::string, std::map<std::string, std::string>>> m{{"HARDWARE\\DESCRIPTION\\System\\BIOS", 
                        {{ "REG_SZ", {{ "SystemSKU", "SystemSKU" }} }}
            }};

Or, use insert

m.insert({"HARDWARE\\DESCRIPTION\\System\\BIOS", 
           {{ "REG_SZ", {{ "SystemSKU", "SystemSKU" }} }} //create inner maps using double braces 
        });

It uses std::map's list constructor (the one that takes a std::initializer_list>).

It is little tricky to read and write though.

Note that the double braces {{ or }} are used together. Inner ones {} marks the single entry in a map, and outer one {} represents the map as a whole.

Liked other answer for simplicity.

Upvotes: 0

Rumpel
Rumpel

Reputation: 101

If you cannot get it to work with initializer lists you could use the classical way:

m["HARDWARE\\DESCRIPTION\\System\\BIOS"]["REG_SZ"]["SystemSKU"] = "SystemSKU";

Hope this helps.

Upvotes: 4

Related Questions