Help, Map of map

N

Noixe

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
 
V

Victor Bazarov

Noixe said:
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
 
R

Rob Williscroft

Noixe wrote in 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.
 
N

Noixe

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 :)
 
R

Rob Williscroft

Noixe wrote in in comp.lang.c++:
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.
 
?

=?iso-8859-1?B?Sm9hcXXtbiBNIEzzcGV6IE118W96?=

Noixe said:
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 said:

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::string("pippo")));
HTH

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
 

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,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top