A map[a,b,c] how to use ?

Jul 11, 2011 at 9:16am
I'd want to have a 3d map.
map[int index, string key, string info_value]

I know that 0 = "key1" , 1 = "key2" etc .
But, How can I access to the 'info_value' using the key ?

Can be the easy solution have two maps ?
map1[int index, string key]
map2[string key, string info_value]
So, it would be very easy :
map2[map1[7]] gives me the 'info_vale'.

But the question is , can I do it with a 3d map ?
Thanks.



Jul 11, 2011 at 10:31am
I don't understand what you're trying to achieve. Is it a map that could have a string or an int as a key? If so, see http://www.boost.org/doc/libs/1_41_0/libs/multi_index/doc/index.html
Jul 11, 2011 at 12:55pm
No.
I have a enum var, so its values are 0,1,2,3 .....
Also I have a map that store the string values of this enum
map[0]="enum_0" and so...

I have a function set_property (My_enum property). Write code for call this function is very easy and understandable. My IDE gives me autocompletion for it (It shows me My_enum elements when I write the code).

Now I have a map that links int values to string values(property from enum) . But, I have to store a value for the property 0,1,2, etc.
So , I could to use another map, or a vector, but I'd want to use a 3d map :
map [int key1, string key2, string value]
I dont want a multimap. Simply I'd want to do a search using the second element of the map. (I'd want a direct method, if not, I prefer to use two maps linked between them )
Thanks
Is it possible ?

Thanks



Last edited on Jul 11, 2011 at 12:56pm
Jul 11, 2011 at 9:15pm
I still don't understand. Try writing code showing how you want to use a 3d map. Maybe that would clarify things.

If you want tu use a string (key2) index and get int and another string, you could use map< string, pair< int, string > > where pair could be replaced with a struct of your own.
If you want to use both a string (key2) and an int (key1) indices (naming them both 'key' implies that you do), multi index map is what you need.
Jul 12, 2011 at 5:48am
I'd want to do searchs on my map using two keys
So mymap[int key1, string key2, value].
But I think I cannot write mymap[]["peter"] (or something like this) isn't it ?
Thanks
Last edited on Jul 12, 2011 at 5:48am
Jul 12, 2011 at 6:24am
You mean a nested map, like map<int, map<string, value_type> >? Something like that is perfectly legal.
Jul 12, 2011 at 6:33am
for a given [a,b,c] if [b,c] are unique pairs, i.e. for each b there will be a unique c, then you can also use a std::pair for b&c and then create a map[a , std::pair<b,c>], the data structure will depend on the nature of a,b &c. You can also wrap b & c in a struct/class and create a map[a, myClass(b,c)] . Nested map might be helpful if for one a, you may have many [b,c] pairs existing. ( same as multi-map).
Jul 12, 2011 at 6:35am
If I understand you correctly then key1 and key2 have a one to one mapping. So it seems to me you want to access your value from either the int key or its corresponding std::string key. Is that right?

If so then I think you are going to need two separate maps. One to map the two keys and another to store the values against one of the keys:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <map>
#include <iostream>

int main()
{
	std::map<int, std::string> keymap;

	keymap[0] = "enum_0";
	keymap[1] = "enum_1";
	keymap[2] = "enum_2";

	std::map<std::string, std::string> m;

	// insert

	m["enum_0"] = "value 0"; // access by std::string
	m[keymap[1]] = "value 1"; // access by int
	m["enum_2"] = "value 2"; // access by std::string

	// retrieve

	std::cout << m[keymap[0]] << '\n'; // access by int
	std::cout << m["enum_1"] << '\n'; // access by std::string
	std::cout << m[keymap[2]] << '\n'; // access by int
}
value 0
value 1
value 2

Is that what you were getting at?
Jul 12, 2011 at 8:32am
Thanks Galik.
This is what I said at the beginning, I need two maps.

Thank you.
Topic archived. No new replies allowed.