473,397 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

enum !

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
Jul 19 '05 #1
11 4822

"Kapil Khosla" <kh*********@yahoo.com> wrote in message
news:91**************************@posting.google.c om...
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
Jul 19 '05 #2
Kapil Khosla wrote:
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;

Jul 19 '05 #3
"Kapil Khosla" <kh*********@yahoo.com> schrieb im Newsbeitrag
news:91**************************@posting.google.c om...
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::ostream& operator <<( std::ostream& o, ADDR e )
{
switch( e )
{
case one: return o << "one";
case two: return o << "two";
case three: return o << "three";
}
}

Georg

Jul 19 '05 #4
kh*********@yahoo.com (Kapil Khosla) wrote in message news:<91**************************@posting.google. com>...
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").
Jul 19 '05 #5
"Chris ( Val )" <ch******@bigpond.com.au> wrote in message
news:be************@ID-110726.news.dfncis.de...

"Kapil Khosla" <kh*********@yahoo.com> wrote in message
news:91**************************@posting.google.c om...
| 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

Jul 19 '05 #6
Thanks a lot guys !
I worked out my solution by using the MAP STL and was pretty interesting.
Kapil
Jul 19 '05 #7

"Jeremy Cowles" <je*************************@asifl.com> wrote in message news:ktgOa.66480

Hi Jeremy.

$i*********@twister.tampabay.rr.com...
| "Chris ( Val )" <ch******@bigpond.com.au> wrote in message
| news:be************@ID-110726.news.dfncis.de...
| >
| > "Kapil Khosla" <kh*********@yahoo.com> wrote in message
| > news:91**************************@posting.google.c om...
| > | 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
Jul 19 '05 #8

"Chris ( Val )" <ch******@bigpond.com.au> wrote in message
news:be************@ID-110726.news.dfncis.de...

"Jeremy Cowles" <je*************************@asifl.com> wrote in message news:ktgOa.66480
Hi Jeremy.

$i*********@twister.tampabay.rr.com...
| "Chris ( Val )" <ch******@bigpond.com.au> wrote in message
| news:be************@ID-110726.news.dfncis.de...
| >
| > "Kapil Khosla" <kh*********@yahoo.com> wrote in message
| > news:91**************************@posting.google.c om...
| > | 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.

Jul 19 '05 #9
In article <be************@ID-110726.news.dfncis.de>, "Chris \( Val \)"
<ch******@bigpond.com.au> 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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #10

"Jerry Coffin" <jc*****@taeus.com> wrote in message
news:MP************************@news.clspco.adelph ia.net...
| In article <be************@ID-110726.news.dfncis.de>, "Chris \( Val \)"
| <ch******@bigpond.com.au> 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
Jul 19 '05 #11
In article <be************@ID-110726.news.dfncis.de>, "Chris \( Val \)"
<ch******@bigpond.com.au> says...

[ ... ]
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>

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum...
21
by: Andreas Huber | last post by:
Hi there Spending half an hour searching through the archive I haven't found a rationale for the following behavior. using System; // note the missing Flags attribute enum Color {
31
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1();...
18
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I have created an enum list like this: enum myEnum : int { This = 2, That, NewVal = 10, LastItm
2
by: Dennis | last post by:
I have an enum as follows: Public Enum myData FirstData = 6 SecondData = 7 end enum Is there anyway that I can return the Enum names by their value, i.e., I want to input 6 into a function...
13
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
10
by: Randy | last post by:
Hi, Can anyone point me to a complete, compilable example of Besser's ENUM++ mechanism? I downloaded it from CUJ and gave it a try but got errors just trying to compile the header enum.h. ...
1
by: Randy | last post by:
Hi, I downloaded and tried the ENUM++ code from CUJ http://www.cuj.com/documents/s=8470/cujboost0306besser/ but can't even get it to compile (see following). I have also downloaded and...
2
by: Randy | last post by:
Hi, I downloaded and tried the ENUM++ code from CUJ http://www.cuj.com/documents/s=8470/cujboost0306besser/ but can't even get it to compile (see following). I have also downloaded and...
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.