Map inside Map

Oct 12, 2011 at 9:09am
Hi,
I have created map inside following way.

Ex: map<key,value> first;

and I have to created second map into following way as per my requirement.

map<first,value> second. So first map is the key value for me in second map.

I have inserted data into both map. So I need to fetch value from second map
on the key(first) basis

first.insert(c,3);
second.insert(first,3).


any pointer?.

Oct 12, 2011 at 9:45am
A map as a key? That seems strange.
In any case, you access a particular element in a map with operator[] or find.
Oct 12, 2011 at 10:06am
little bit more info why are we using this map.

Aplication is passing two variable as input. So I have create first map. than we need to fetch
another value from the database.

like : select value from temdb where k='key1' and value=='value';

So created second map like this <first,value>. it contain the database result .

So if anyone want to know the value he does not need to hit database.

So I am able to insert data successful but I need to fetch value with respective to first one.

I hope it clairy my issue.
Oct 12, 2011 at 11:15am
Aplication is passing two variable as input. So I have create first map.

Why are you storing them in a map instead of a std::pair?
Last edited on Oct 12, 2011 at 11:17am
Oct 12, 2011 at 12:21pm
hmm I was not aware of std::pair. But I did one work around for fix this issue but not sure
coding efficiency.

sample code

int data( int key,int value)
{
map<key,value> first;
first.insert(key,value);


int value2 = get_data_base(.. select value2 from tempdb where key=key and and value=value);

map<first,value2> second;
second.insert(first,value2);

second::iterator sec;

sec = second.find(first);

cout<<sec->value;

So I got the value this way... not sure how much I am correct but during unit testing no issue.


Topic archived. No new replies allowed.