map within map

M

ma740988

Consider

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <utility>
namespace
{

typedef std::map< std::string, std::string > my_map_type;

typedef std::map< std::string, my_map_type >
my_double_map_type;

void print_leaf(
my_map_type::value_type const & v)
{
std::cout << " + " << v.first << " : " << v.second <<
std::endl;
}

void print_root(
my_double_map_type::value_type const & v)
{
std::cout << v.first << std::endl;

std::for_each(
v.second.begin(),
v.second.end(),
print_leaf);
}

void f()
{
my_double_map_type l_map;
l_map["Test"]["one"] = "Element";
l_map["Test"]["two"] = "Element2";
l_map["Test"]["one"] = "Element3"; // overwrites "Element"
l_map["Test2"]["one"] = "Element";

std::for_each(
l_map.begin(),
l_map.end(),
print_root);

}
}

int main(int, char*)
{
f();
return EXIT_SUCCESS;
}

Two observations per teh source puzzles me.
1.
void f()
{
const my_double_map_type l_map; // add const qualifier
// stuff
}
The addition of the const qualifer is forbidden, and I'm not sure if I
understand why?

2.

my_map_type my;
my["str1"] = "str2"; // produces (str1, str2), No qualms here.

Now

my_double_map_type l_map
l_map["str1"]["str2"] = "str3"; //produces (str1, str2, str3) but
the KEY here is str1. The value is str2, str3 hence I've often
wondered why not something akin to:
l_map["str1"] = ("str2, str3"); //<<-- yes, this makes no sense.

In the end this when viewed from a map within a map, this,
l_map["str1"]["str2"] = "str3"; doesn't look like KEY/VALUE pair to
me.

Perhaps it's time to revisit Josuttis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top