Solved

mapping enums to sring

Posted on 2011-03-22
8
333 Views
Last Modified: 2012-05-11
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
Comment
Question by:gname
8 Comments
 
LVL 5

Expert Comment

by:jaiminpsoni
ID: 35192594
0
 
LVL 5

Expert Comment

by:jaiminpsoni
ID: 35192683
Or may be simple lookup table?

Or std::map may also help...
0
 

Author Comment

by:gname
ID: 35194146
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 5

Expert Comment

by:jaiminpsoni
ID: 35196177
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
 
LVL 1

Expert Comment

by:thangdn
ID: 35246861
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
 
LVL 5

Accepted Solution

by:
TomasP earned 500 total points
ID: 35317870
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
 
LVL 100

Expert Comment

by:mlmcc
ID: 36275168
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

The greatest common divisor (gcd) of two positive integers is their largest common divisor. Let's consider two numbers 12 and 20. The divisors of 12 are 1, 2, 3, 4, 6, 12 The divisors of 20 are 1, 2, 4, 5, 10 20 The highest number among the c…
Prime numbers are natural numbers greater than 1 that have only two divisors (the number itself and 1). By “divisible” we mean dividend % divisor = 0 (% indicates MODULAR. It gives the reminder of a division operation). We’ll follow multiple approac…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now