enumerations: Possible to get the "key" string?

G

Georg J. Stach

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");
.
.
.
}



Thanks for replies
Georg
 
?

=?ISO-8859-1?Q?Andr=E9_Kempe?=

Georg said:
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 ];
 
B

Bob Hairgrove

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.
 
B

Bob Hairgrove

Georg said:
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.
 
G

Greg

Bob said:
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
 
G

Georg J. Stach

Greg said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top