f



map within map

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
0
ma740988926 (153)
11/7/2004 1:15:48 AM
comp.lang.c++ 49423 articles. 7 followers. Post Follow

0 Replies
688 Views

Similar Articles

[PageSpeed] 38

Reply: