I’m new to C++ and I recently found out that we can initialize a collection of strings by using the following code:
map <string,string> myArray;
myArray["key1"] = "value1";
myArray["key2"] = "value2";
cout<<myArray["key1"]<<endl; //result: value1
But what if I have to assign to key2 not a string but another collection? Here is how I suppose it would look:
map <string, ???another map???> myArray;
map<string,string> secondArray;
secondArray["foo"] = "bar";
myArray["key1"] = "value1";
myArray["key2"] = secondArray;
cout<<myArray["key2"]["foo"]<<endl; //expected result: bar
Is that possible?
As the questioner seems to have accepted one of the answers, this is just a
supplement.
If
Boostis allowed, Boost.Variant might meet the purpose.For example, the following code prints
baras in the question.Here is a test on ideone.