By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,502 Members | 1,614 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,502 IT Pros & Developers. It's quick & easy.

enumerations: Possible to get the "key" string?

P: n/a
Hello,

taken I've declared an enumeration like this:

enum Color{ red,green,blue };

The internal representation of Color is of type integer and it's no problem
to print out their values.

But is it possible to get their string representing value _without_ having a
switch like this?

switch (Color) {
case (red):
string str("red");
Nov 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Georg J. Stach wrote:
Hello,

taken I've declared an enumeration like this:

enum Color{ red,green,blue };

The internal representation of Color is of type integer and it's no problem
to print out their values.

But is it possible to get their string representing value _without_ having a
switch like this?

switch (Color) {
case (red):
string str("red");
.
.
.
}


const char* str_table[ ] = { "red" , "green" , "blue" };
string str = str_table[ color ];
Nov 22 '05 #2

P: n/a
On Sun, 13 Nov 2005 11:34:51 +0100, "Georg J. Stach"
<VO****************@gmx.de> wrote:
Hello,

taken I've declared an enumeration like this:

enum Color{ red,green,blue };

The internal representation of Color is of type integer and it's no problem
to print out their values.

But is it possible to get their string representing value _without_ having a
switch like this?

switch (Color) {
case (red):
string str("red");
.
.
.
}


There is no built-in way of doing this. One way would be to store the
strings in a std::map<Color,std::string>, then you could use the map's
find() function to retrieve the string. Alternatively, since you don't
assign values to the enumeration items yourself, the enum value is
equivalent to a sequential index. In that case you could also use a
vector<string> and use the enum value directly as an index into the
vector.

--
Bob Hairgrove
No**********@Home.com
Nov 22 '05 #3

P: n/a
On Sun, 13 Nov 2005 11:50:36 +0100, André Kempe <AK****@the-map.de>
wrote:
Georg J. Stach wrote:
Hello,

taken I've declared an enumeration like this:

enum Color{ red,green,blue };

The internal representation of Color is of type integer and it's no problem
to print out their values.

But is it possible to get their string representing value _without_ having a
switch like this?

switch (Color) {
case (red):
string str("red");
.
.
.
}


const char* str_table[ ] = { "red" , "green" , "blue" };
string str = str_table[ color ];


Of course, this will not work if the enum values are not sequential
beginning with 0 (i.e. automatically generated). But it would work in
this special case.

--
Bob Hairgrove
No**********@Home.com
Nov 22 '05 #4

P: n/a
Bob Hairgrove wrote:
On Sun, 13 Nov 2005 11:34:51 +0100, "Georg J. Stach"
<VO****************@gmx.de> wrote:
Hello,

taken I've declared an enumeration like this:

enum Color{ red,green,blue };

The internal representation of Color is of type integer and it's no problem
to print out their values.

But is it possible to get their string representing value _without_ having a
switch like this?

switch (Color) {
case (red):
string str("red");
.
.
.
}


There is no built-in way of doing this. One way would be to store the
strings in a std::map<Color,std::string>, then you could use the map's
find() function to retrieve the string. Alternatively, since you don't
assign values to the enumeration items yourself, the enum value is
equivalent to a sequential index. In that case you could also use a
vector<string> and use the enum value directly as an index into the
vector.


A std::map also supports indexing of its values with its keys:

#include <map>
#include <iostream>

enum Color { red, green, blue };

int main()
{
std::map< Color, std::string > colorMap;

colorMap[ red ] = "red";
colorMap[ blue ] = "blue";
colorMap[ green ] = "green";

std::cout << colorMap[ red ]; // prints red
std::cout << colorMap[ blue ]; // prints blue
...
}

Personally, I find the indexing more readable than calls to map::
find().

Greg

Nov 22 '05 #5

P: n/a
Greg wrote:
enum*Color*{*red,*green,*blue*};

int*main()
{
std::map<*Color,*std::string*>*colorMap;

colorMap[*red*]**=*"red";
colorMap[*blue*]*=*"blue";
colorMap[*green*]*=*"green";
...
}

Thanks Greg and all who have also replied,

I suppose for my application this is the most robust and efficient solution,
as I'm able to sort the map and do some other operations on it.

--
Cheers
Georg
Nov 22 '05 #6

P: n/a
In article <dl**********@news01.versatel.de>,
Georg J. Stach <VO****************@gmx.de> wrote:
taken I've declared an enumeration like this:

enum Color{ red,green,blue };

The internal representation of Color is of type integer and it's no problem
to print out their values.

But is it possible to get their string representing value _without_ having a
switch like this?

switch (Color) {
case (red):
string str("red");
.
.
.
}


Check out http://www.comeaucomputing.com/techtalk/#enumtostring
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Nov 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.