Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 366
  • Last Modified:

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
0
gname
Asked:
gname
1 Solution
 
jaiminpsoniCommented:
0
 
jaiminpsoniCommented:
Or may be simple lookup table?

Or std::map may also help...
0
 
gnameAuthor Commented:
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 ??
0
[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

 
jaiminpsoniCommented:
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/

0
 
thangdnCommented:
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","March"};

int main() {
  Month  m= Jan;
  printf("The month is %s", name[m - 1]);
}
0
 
TomasPCommented:
To save all of the typing use your preprocessor to directly turn the text for each enum value into a string
#define TOSTRING( aenum ) # aenum

enum {
Jan =1,
Feb,
March,
April
}


To leverage the proprocessor just use TOSTRING like this:

TOSTRING(Feb);   // This will resolve to the text 'Feb'
You can use this directly or declare a table.

You can populate a string table in a loop without typing it all in by following the "tricks" used here
http://www.edm2.com/0405/enumeration.html

Once you get the framework up, you will reuse this without a doubt, I do


0
 
mlmccCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now