gname
asked on
mapping enums to sring
Hi ,
I have around 500 enums , which I need to print as string vlaue.
for example -
enum {
Jan =1,
Feb,
March,
April
}
int month =2
switch(month)
case Jan:
cout <<January <<endl;
case Feb...
cout <<Feburay <<endl
What is best way to map enum to strings, becasue switch case is too much for 500 enums ?
Thanks
I have around 500 enums , which I need to print as string vlaue.
for example -
enum {
Jan =1,
Feb,
March,
April
}
int month =2
switch(month)
case Jan:
cout <<January <<endl;
case Feb...
cout <<Feburay <<endl
What is best way to map enum to strings, becasue switch case is too much for 500 enums ?
Thanks
Or may be simple lookup table?
Or std::map may also help...
Or std::map may also help...
ASKER
I can prepare a map and then search, but the question here is - how to insert string as enum vlaue names in the map? I dont want to do one by one mapping for all 500 vlaues. is there any template method etc ??
OK... So, I am not sure if I really understood your question....
I thought you wanted to make retrieval efficient... but here it seems you want to make "writing the code" as efficient.
Are you looking for something like this?
http://www.cplusplus.com/forum/general/2949/
I thought you wanted to make retrieval efficient... but here it seems you want to make "writing the code" as efficient.
Are you looking for something like this?
http://www.cplusplus.com/forum/general/2949/
Hi, you can use an array of strings to store names. I illustrate one example in the following code. The other way would be store all the names in a file and read them into an array. I can write a code if you want the solution reading from a file. Hope it helps.
enum Month {Jan=1, Feb, Mar};
char* name[] = {"January","February","Mar ch"};
int main() {
Month m= Jan;
printf("The month is %s", name[m - 1]);
}
enum Month {Jan=1, Feb, Mar};
char* name[] = {"January","February","Mar
int main() {
Month m= Jan;
printf("The month is %s", name[m - 1]);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
http://en.wikipedia.org/wiki/Hash_map_(C%2B%2B)