?
Solved

mapping enums to sring

Posted on 2011-03-22
8
Medium Priority
?
359 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 
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 2000 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 101

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

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.

Question has a verified solution.

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

Article by: Nadia
Suppose you use Uber application as a rider and you request a ride to go from one place to another. Your driver just arrived at the parking lot of your place. The only thing you know about the ride is the license plate number. How do you find your U…
Article by: Nadia
Linear search (searching each index in an array one by one) works almost everywhere but it is not optimal in many cases. Let's assume, we have a book which has 42949672960 pages. We also have a table of contents. Now we want to read the content on p…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

752 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