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 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
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;
"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 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").
"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
Thanks a lot guys !
I worked out my solution by using the MAP STL and was pretty interesting.
Kapil
"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
"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.
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.
"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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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
{
|
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();...
|
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
|
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...
|
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)
|
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.
...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |