Solved

mapping enums to sring

Posted on 2011-03-22
8
341 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

776 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