Connecting Tech Pros Worldwide Forums | Help | Site Map

Help, Map of map

Noixe
Guest
 
Posts: n/a
#1: Jul 23 '05
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





Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Help, Map of map


"Noixe" <NoixeTOGLIMI@email.it> wrote...[color=blue]
> In this source
>
> #include <iostream>
> #include <map>
> #include <string>[/color]

using namespace std;
[color=blue]
>
> 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?[/color]

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.
[color=blue]
> How I get the key and value of internal map?[/color]

You need to supply both keys:

PG["pippo"]["pluto"]

Victor


Rob Williscroft
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Help, Map of map


Noixe wrote in news:41d6d163@x-privat.org in comp.lang.c++:
[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]

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/
Noixe
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Help, Map of map


"Victor Bazarov" <v.Abazarov@comAcast.net> ha scritto:
[color=blue]
> PG["pippo"]["pluto"][/color]

If I want get "pluto" ?


Noixe
Guest
 
Posts: n/a
#5: Jul 23 '05

re: Help, Map of map


"Rob Williscroft" <rtw@freenet.co.uk> ha scritto:

[color=blue]
> 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;[/color]

Yes but I have also other elements in PG, not only one.
[color=blue]
> But I get the impression from what you say that maybe you
> really want:[/color]

No, I need a Map :)


Rob Williscroft
Guest
 
Posts: n/a
#6: Jul 23 '05

re: Help, Map of map


Noixe wrote in news:41d6d8b4@x-privat.org in comp.lang.c++:
[color=blue]
> "Rob Williscroft" <rtw@freenet.co.uk> ha scritto:
>
>[color=green]
>> 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;[/color]
>
> Yes but I have also other elements in PG, not only one.
>[/color]

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/
Joaquín M López Muñoz
Guest
 
Posts: n/a
#7: Jul 23 '05

re: Help, Map of map


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

Closed Thread