[C++]Nested map problem.

HDS

Expert Member
Joined
Mar 3, 2013
Messages
1,849
Reaction score
2
I can't for the life of me figure out what to do. Basically, I have to use a nested map to store a series variables a, b, c. The first var a is the key, and the object is the nested map ( b, c, where b is the key of the nested map and c is the object of the nested map ). After some values were inserted into the map, I have to find a user specified variable a within the map. So, for instance, if a user wants to search all the records with the name "Eric" ( var a ), I have to display all the records with the a value "Eric".

I have to declare it like this :

typedef map < string, map<string, int> > my_map;
my_map map_find;

Which would make : map< a, map<b, c> > my_map

Using outer and inner map not allowed. Using a multimap is not allowed.

Now comes the problem. I am supposed to input a few records with the same variable a. For instance :

do
{
cout <<" a \n";
cin >> a;
cout <<" b \n";
cin >> b;
cout <<" c " <<endl
cin >> c;

map_find[name].insert = ( make_pair ( surname, tel_no ) );
cin >> choice;

}while ( choice == 'y' || choice == toupper('y') );

It loops until you input anything other than y/Y . When prompted to input a value for a, I input "boob" 2 times. The problem is, it only records the first "boob", since a normal map doesn't allow duplicate keys. But I am supposed to input multiple keys with the value "boob".

I don't know how clearly I explained it, but basically I need to insert duplicate keys in a nested map.
 
Its working as it should. The keys in a map are unique, hence only one occurrence of "boob". As D3x! said, change your design.
 
There is no logical reason for having a map (or hack thereof) if you can have duplicate keys. Say you were able to add two instances of boob, what would be the purpose? You won't know which is which when you retrieve it.

You either need to use a different field if a is part of an entity, or if it's not, rethink your design as D3x! said.
 
map only supports unique keys, you'd need to use multimap, but as you said you can't. You could perhaps make the key a unique object and give it a name or whatever variable, and a unique field that could be a guid or anything. That way the equality comparer should not see it as unique (assumption).
 
Last edited:
my opinion, you need to change your design, what exactly is the goal here?

If I read the OP's post correctly, I think s/he is trying to create a 3 dimensional map, where e.g. the first key is the name ("Eric") and second could be surnames of all people named "Eric".

-G-
 
If I read the OP's post correctly, I think s/he is trying to create a 3 dimensional map, where e.g. the first key is the name ("Eric") and second could be surnames of all people named "Eric".

-G-

No, then he would only be required to store a single instance of "Eric", but hes trying to store multiple.
 
You definitely need a different data structure. Hashtable which maps to dictionaries maybe, i.e. "Bob" maps to several dictionaries containing surnames and telephone numbers? Will duplicate surnames be possible?
 
I think if you give us a use case of exactly what you want the code to do, we will be able to help you much better.
 
I'm even more confused now. Might be a mistake in the book.

map only supports unique keys, you'd need to use multimap, but as you said you can't. You could perhaps make the key a unique object and give it a name or whatever variable, and a unique field that could be a guid or anything. That way the equality comparer should not see it as unique (assumption).

Hmmm, a composite key maybe? If I were to make a pair of a and b, something like my_map[make_pair(a,b)] = c; would that work? Keeping in mind the map declaration? Can one use a pair as a key?
 
I'm even more confused now. Might be a mistake in the book.



Hmmm, a composite key maybe? If I were to make a pair of a and b, something like my_map[make_pair(a,b)] = c; would that work? Keeping in mind the map declaration? Can one use a pair as a key?

Please post the description from the book.
 
Oh, reading your first statement again, I have a better idea of what you are trying to do.

Surely the following would work?
++map["Eric"][Surname"];

The first set of square brackets gets you to the inner map, and the second set of square brackets then gets you to the int in question.
 
I have a pretty simple way you can do what i think you want with a std map, but this seems very much like an assignment type question :whistle:
 
I have a pretty simple way you can do what i think you want with a std map, but this seems very much like an assignment type question :whistle:

What might seem simple, could cost more O time.
 
Please post the description from the book.

This is an old exercise I stumbled upon, needing to borrow code from it for another exercise. It's from one of the Deitel books, don't remember the exact details but here is the outline :

Var a should be stored in a map. This map has a string ( a ) as the key and another map as the object. The inner map will have a string as a key ( b ) and an integer as the object ( c ). Having a map inside another map will enable you to have records with the same a value to be added. The map has to be declared as follows : map<string, map<string, int> >. Use the find function to find all the records that specify the user search criteria ( the a value that the user specifies ).

Oh, reading your first statement again, I have a better idea of what you are trying to do.

Surely the following would work?
++map["Eric"][Surname"];

The first set of square brackets gets you to the inner map, and the second set of square brackets then gets you to the int in question.

That won't work. The second bracket has 2 elements, b and c. So when you iterate through them you will have outer_iterator -> first, inner_iterator -> first, inner_iterator -> second. The second element, in this case surname, will have 2 elements on it's own, the surname and let's say ID number.
 
:p

the goal is to have a directory yes? i.e search on name and get the applicable names with surname and telephone number? do you have to use a map <string, map<string, int>>? or could you use say a map<string, someClass> ?

edit: well does the key have to be the first name? or could it be first name and surname or first name and initial etc?
 
Last edited:
:p

the goal is to have a directory yes? i.e search on name and get the applicable names with surname and telephone number? do you have to use a map <string, map<string, int>>? or could you use say a map<string, someClass> ?

edit: well does the key have to be the first name? or could it be first name and surname or first name and initial etc?

Can't use a class. The map declaration also cannot change. Are you suggesting a composite key? Like, for instance, name + surname?
 
:p

the goal is to have a directory yes? i.e search on name and get the applicable names with surname and telephone number? do you have to use a map <string, map<string, int>>? or could you use say a map<string, someClass> ?

edit: well does the key have to be the first name? or could it be first name and surname or first name and initial etc?

That solves nothing, the issue is with map<string. Map does not allow multiple of the same key.

HDS - I believe the question or excerise is being misunderstood, you will need to group the values for the particular key,
while you may believe you need to insert two boobs, it will be in fact one, the the corresponding map to boob would now have two entrys

['boob',['x',y]['c','d']], which then you can just keep appending.

But yeah basically a composite key could work, std::make_pair(x, y)
 
Last edited:
Top
Sign up to the MyBroadband newsletter
X