I have a map, where string representing the name of attribute and the second value representing the type, which this attribute should have.
map.insert(make_pair("X", iDatatype::iUint));
map.insert(make_pair("Y", iDatatype::iUint));
map.insert(make_pair("RADIANT", iDatatype::iFloat));
where iDatatype is just enumeration of all possible types.
typedef enum
{
iUnknown = 0,
iByte = 1,
iChar = 2,
iUChar = 3,
iShort = 4.....
} iDatatype;
If the program gets the command to create, for example, “RADIANT” than it look at the map, find the iDatatype value (iter->second) and go to switch case.
switch (iter->second) {
case iDatatype::iUint:
uint value = ......
// You gotta do what you gonna do
break;
} .......
In Switch case, the function, which depends on type of attribute, will be called.
This code works. But I am not sure, if it the best solution to map string with the types.
And the problem that I don’t know what should I look for? Could you recommend what methods or techniques are commonly used for such purpose? Thank you a lot.
Unless you need the map for some other reference, another approach will be:
However I’m not sure how much faster this will be than using a map, because a map uses binary search while this uses iterative search.
If you want to gain the boost of binary search while no need for another enum you can use a map of functions:
and later call it like:
you can also pass parameters to it like: