enum !

K

Kapil Khosla

Hi,
I have a code in which I have an enum like

enum ADDR
{
one = 0;
two = 1;
three = 2;
};

Later in code , I have a function that returns an ADDR type, which
returns an integer referring to the correct value but I want to print
out the name one,two or three.
How can I do that, my example has a lot of cases so dont want to use a
switch statement.

Thanks,
Kapil
 
J

Josephine Schafer

Kapil Khosla said:
Hi,
I have a code in which I have an enum like

enum ADDR
{
one = 0;
two = 1;
three = 2;
};

Later in code , I have a function that returns an ADDR type, which
returns an integer referring to the correct value but I want to print
out the name one,two or three.
How can I do that, my example has a lot of cases so dont want to use a
switch statement.

AFAIK it's not achievable in your case (with an enum).
Why not use a map?

With best wishes,
J.Schafer
 
R

Rolf Magnus

Kapil said:
Hi,
I have a code in which I have an enum like

enum ADDR
{
one = 0;
two = 1;
three = 2;
};

Later in code , I have a function that returns an ADDR type, which
returns an integer referring to the correct value but I want to print
out the name one,two or three.
How can I do that, my example has a lot of cases so dont want to use a
switch statement.

You can make an array of the string representations:

const char* ADDR_strings[] = { "one", "two", "three" };

Then you can do something like:

ADDR addr = get_the_addr();
std::cout << ADDR_strings[addr] << std::endl;
 
G

Georg Krichel

Kapil Khosla said:
Hi,
I have a code in which I have an enum like

enum ADDR
{
one = 0;
two = 1;
three = 2;
};

Later in code , I have a function that returns an ADDR type, which
returns an integer referring to the correct value but I want to print
out the name one,two or three.
How can I do that, my example has a lot of cases so dont want to use a
switch statement.

Thanks,
Kapil

I dont' know any solution without a 'switch' or a std::map with an insert
for each enumerator.
In my applications I use something like this:

std::eek:stream& operator <<( std::eek:stream& o, ADDR e )
{
switch( e )
{
case one: return o << "one";
case two: return o << "two";
case three: return o << "three";
}
}

Georg
 
Y

yvan joffre

Hi,
I have a code in which I have an enum like

enum ADDR
{
one = 0;
two = 1;
three = 2;
};

Later in code , I have a function that returns an ADDR type, which
returns an integer referring to the correct value but I want to print
out the name one,two or three.
How can I do that, my example has a lot of cases so dont want to use a
switch statement.

Thanks,
Kapil


Can you modify the definition of ADDR? If yes, you can try the
following code :
#define FOR_ALL_ADDR(P) P(one) P(two) P(three)
#define DECLARE_ADDR(A) A,

enum ADDR {
FOR_ALL_ADDR(DECLARE_ADDR) ADDR_COUNT
};

#define MAP_ADDR_TO_STRING(A) #A,
const char *const AddrStr[ADDR_COUNT] = {
FOR_ALL_ADDR(MAP_ADDR_TO_STRING)
};

Then, you index directly the array AddrStr with an ADDR value to
retrieve the associated string (for instance AddrStr[one] is equals to
"one").
 
J

Jeremy Cowles

Chris ( Val ) said:
| Hi,
[snip]

int main()
{
typedef std::map<ADDR, std::string> MyMap;

MyMap M;


Couldn't you use the following code, eliminating the need for a typedef?

std::map<ADDR, std::string> M;

Or do you create a typedef for later use so you don't have to keep writing
out the template parameters?

Thanks,
Jeremy
 
K

Kapil Khosla

Thanks a lot guys !
I worked out my solution by using the MAP STL and was pretty interesting.
Kapil
 
C

Chris \( Val \)

Hi Jeremy.

[email protected]...
| | >
| > | > | Hi,
|
| [snip]
|
| > int main()
| > {
| > typedef std::map<ADDR, std::string> MyMap;
| >
| > MyMap M;
|
|
| Couldn't you use the following code, eliminating the need for a typedef?
|
| std::map<ADDR, std::string> M;

Yes, of course :).

| Or do you create a typedef for later use so you don't have to keep writing
| out the template parameters?

Correct, but mainly(for this exercise), to *avoid* stuff like this:

MyMap.insert( std::map<ADDR, std::string>::value_type( one, "one" ) );
MyMap.insert( std::map<ADDR, std::string>::value_type( two, "two" ) );
MyMap.insert( std::map<ADDR, std::string>::value_type( three, "three" ) );

....rather than:

M.insert( MyMap::value_type( one, "one" ) );
M.insert( MyMap::value_type( two, "two" ) );
M.insert( MyMap::value_type( three, "three" ) );

See the difference ?

Cheers.
Chris Val
 
J

Jeremy Cowles

Chris ( Val ) said:
Hi Jeremy.

[email protected]...
| | >
| > | > | Hi,
|
| [snip]
|
| > int main()
| > {
| > typedef std::map<ADDR, std::string> MyMap;
| >
| > MyMap M;
|
|
| Couldn't you use the following code, eliminating the need for a typedef?
|
| std::map<ADDR, std::string> M;

Yes, of course :).

| Or do you create a typedef for later use so you don't have to keep writing
| out the template parameters?

Correct, but mainly(for this exercise), to *avoid* stuff like this:

MyMap.insert( std::map<ADDR, std::string>::value_type( one, "one" ) );
MyMap.insert( std::map<ADDR, std::string>::value_type( two, "two" ) );
MyMap.insert( std::map<ADDR, std::string>::value_type( three, "three" ) );

...rather than:

M.insert( MyMap::value_type( one, "one" ) );
M.insert( MyMap::value_type( two, "two" ) );
M.insert( MyMap::value_type( three, "three" ) );

See the difference ?


Yes, thanks.
 
J

Jerry Coffin

[ ... ]
Correct, but mainly(for this exercise), to *avoid* stuff like this:

MyMap.insert( std::map<ADDR, std::string>::value_type( one, "one" ) );
MyMap.insert( std::map<ADDR, std::string>::value_type( two, "two" ) );
MyMap.insert( std::map<ADDR, std::string>::value_type( three, "three" ) );

...rather than:

M.insert( MyMap::value_type( one, "one" ) );
M.insert( MyMap::value_type( two, "two" ) );
M.insert( MyMap::value_type( three, "three" ) );

See the difference ?

For this particular set of circumstances, I'd use:

M[one] = "one";
M[two] = "two";
M[three] = "three";

Which is not only shorter still, but strikes me as considerably more
readable as well. Of course, it doesn't apply to all the possible
collection types, but for the job at hand it works very nicely.
 
C

Chris \( Val \)

| In article <[email protected]>, "Chris \( Val \)"
| <[email protected]> says...
|
| [ ... ]
|
| > Correct, but mainly(for this exercise), to *avoid* stuff like this:
| >
| > MyMap.insert( std::map<ADDR, std::string>::value_type( one, "one" ) );
| > MyMap.insert( std::map<ADDR, std::string>::value_type( two, "two" ) );
| > MyMap.insert( std::map<ADDR, std::string>::value_type( three, "three" ) );
| >
| > ...rather than:
| >
| > M.insert( MyMap::value_type( one, "one" ) );
| > M.insert( MyMap::value_type( two, "two" ) );
| > M.insert( MyMap::value_type( three, "three" ) );
| >
| > See the difference ?
|
| For this particular set of circumstances, I'd use:
|
| M[one] = "one";
| M[two] = "two";
| M[three] = "three";
|
| Which is not only shorter still, but strikes me as considerably more
| readable as well. Of course, it doesn't apply to all the possible
| collection types, but for the job at hand it works very nicely.

Hi Jerry.

Agreed, but as I said, I'm trying to get into the habit of
using 'value_type' :).

Cheers.
Chris Val
 
J

Jerry Coffin

[ ... ]
Hi Jerry.

Hi Chris,
Agreed, but as I said, I'm trying to get into the habit of
using 'value_type' :).

If I might venture an opinion, I'd advance one habit that I think should
outweigh the rest: writing the best code you know how in any given
situation. Particular techniques are useful to the degree that they
help you program better, but (still IMO, of course) should be ignored
when they get in the way of programming as well as possible.

OTOH, there's no real question that when you learn a new technique, it's
sometimes easy to overdo it a bit. At times, it's even useful to do so,
because it helps you explore not only the uses, but also the limits and
weaknesses of that technique -- and 2:00 AM the morning before the big
demo is NOT the best time for "educational experiences" about the limits
of a technique you've been counting on to make things work! <G>
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top