f



Help, Map of map

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?

Thanks




0
1/1/2005 4:35:47 PM
comp.lang.c++ 49423 articles. 6 followers. Post Follow

6 Replies
679 Views

Similar Articles

[PageSpeed] 11

"Noixe" <NoixeTOGLIMI@email.it> wrote...
> In this source
>
> #include <iostream>
> #include <map>
> #include <string>

using namespace std;

>
> 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?

You can use an auxiliary map object:

  map<string,int> aux;
  aux["pluto"] = 6;

then add it to the main map:

  PG["pippo"] = aux;

a copy of 'aux' will be made, you don't have to worry about 'aux's
lifetime being shorter than 'PG's.

> How I get the key and value of internal map?

You need to supply both keys:

  PG["pippo"]["pluto"]

Victor 


0
v.Abazarov (13256)
1/1/2005 4:52:46 PM
Noixe wrote in news:41d6d163@x-privat.org in comp.lang.c++:

> 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?
> 

This works:


#include <iostream>
#include <ostream>
#include <map>
#include <string>


int main() 
{
  using namespace std;

  map<string, map <string, int> > PG;
  PG[ "pippo" ][ "pluto" ] = 6;
  
  cout << PG["pippo"].begin()->first << endl; 
  cout << PG["pippo"].begin()->second << endl; 
}

But I get the impression from what you say that maybe you
really want:

#include <iostream>
#include <ostream>
#include <map>
#include <string>
#include <utility>


int main() 
{
  using namespace std;

  map< string, pair< string, int > > PG;
  
  PG[ "pippo" ] = pair< string, int >( "pluto", 6 );
  
  cout << PG[ "pippo" ].first << endl;
  cout << PG[ "pippo" ].second << endl;
}

HTH.

Rob.
-- 
http://www.victim-prime.dsl.pipex.com/
0
rtw1 (575)
1/1/2005 4:55:30 PM
"Victor Bazarov" <v.Abazarov@comAcast.net> ha scritto:

>   PG["pippo"]["pluto"]

If I want get "pluto" ?


0
1/1/2005 5:02:04 PM
"Rob Williscroft" <rtw@freenet.co.uk> ha scritto:


> int main()
> {
>   using namespace std;
>
>   map<string, map <string, int> > PG;
>   PG[ "pippo" ][ "pluto" ] = 6;
>
>   cout << PG["pippo"].begin()->first << endl;
>   cout << PG["pippo"].begin()->second << endl;

Yes but I have also other elements in PG, not only one.

> But I get the impression from what you say that maybe you
> really want:

No, I need a Map :)


0
1/1/2005 5:07:00 PM
Noixe wrote in news:41d6d8b4@x-privat.org in comp.lang.c++:

> "Rob Williscroft" <rtw@freenet.co.uk> ha scritto:
> 
> 
>> int main()
>> {
>>   using namespace std;
>>
>>   map<string, map <string, int> > PG;
>>   PG[ "pippo" ][ "pluto" ] = 6;
>>
>>   cout << PG["pippo"].begin()->first << endl;
>>   cout << PG["pippo"].begin()->second << endl;
> 
> Yes but I have also other elements in PG, not only one.
> 

Then you need to iterate over the (inner) map:

  map< string, int >::iterator ptr, lim;

  ptr = PG[ "pippo" ].begin();
  lim = PG[ "pippo" ].end();

  for (; ptr != lim; ++ptr )
  {
    cout << ptr->first << " = " << ptr->second << '\n';
  }   

If you know the inner key you're looking for:

  cout << [ "pippo" ][ "pluto" ] << '\n';

Rob.
-- 
http://www.victim-prime.dsl.pipex.com/
0
rtw1 (575)
1/1/2005 5:22:57 PM
Noixe wrote:
> 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?
>

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/doc/advanced_topics.html#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_string>,
member<PG_entry,std::string,&PG_entry::second_string>
>
>
>
> PG_t;

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=3D
PG.equal_range(make_tuple(std::string("pluto")));
while(p.first!=3Dp.second){
//...
++p.first;
}

Or, to obtain a particular entry knowing its two keys:

PG_t::iterator it=3D
PG.find(make_tuple(std::string("pluto"),std::string("pippo")));
HTH

Joaqu=EDn M L=F3pez Mu=F1oz
Telef=F3nica, Investigaci=F3n y Desarrollo

0
joaquin (21)
1/1/2005 7:56:01 PM
Reply: