Noixe wrote:[color=blue]
> Hello,
>
> I'm italian then sorry for my bad english:
>
> In this source
>
> #include <iostream>
> #include <map>
> #include <string>
>
> map <string, map <string, int> > PG;
>
> int main() {
> PG.insert (map <string, map<string, int> >::value_type ("pippo",
> map<string, int>::value_type ("pluto", 6))); // Error
> std::cout << PG["pippo"].first << std::endl; // Error
> return 0;
> }
>
> How can I insert a key and value in a internal map?
>
> How I get the key and value of internal map?
>[/color]
Hi Noixe,
To add to other responses you've gotten involving maps
of maps, allow me to suggest you take a look at the
Boost Multi-index Containers library, which supports a
notion of *composite keys* which can be used to
obtain the kind of data structure you're after.
Boost.MultiIndex composite keys are discussed at
http://boost.org/libs/multi_index/do...composite_keys
Your example can be formulated in Boost.MultiIndex
like follows:
struct PG_entry
{
std::string first_string;
std::string second_string;
int value;
};
typedef multi_index_container<
PG_entry,
indexed_by<
ordered_unique<
composite_key<
PG_entry,
member<PG_entry,std::string,&PG_entry::first_strin g>,
member<PG_entry,std::string,&PG_entry::second_stri ng>[color=blue]
>
>
>
> PG_t;[/color]
PG_t PG;
Then, to obtain all entries whose first string is "pluto"
you can write:
std::pair<PG_t::iterator,PG_t::iterator> p=
PG.equal_range(make_tuple(std::string("pluto")));
while(p.first!=p.second){
//...
++p.first;
}
Or, to obtain a particular entry knowing its two keys:
PG_t::iterator it=
PG.find(make_tuple(std::string("pluto"),std::strin g("pippo")));
HTH
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo