Hello All
I have a enum decla like this
enum Days // Declare enum type Days
{
saturday, // saturday = 0 by default
sunday = 0, // sunday = 0 as well
monday, // monday = 1
tuesday, // tuesday = 2
wednesday, // etc.
thursday,
friday
};
and am declaring a var
enum Days today = sunday;
Can I iterate enum Days, using any loops/ other methods
Thanks 8 8305
On Wed, 11 Aug 2004 12:39:42 +0530, "Imran" <im******@in.bo sch.com>
wrote: Hello All
I have a enum decla like this enum Days // Declare enum type Days { saturday, // saturday = 0 by default sunday = 0, // sunday = 0 as well
(suggestion: you can also write:
sunday = saturday
in case you ever want to change the value of saturday
but keep sunday in synch.) monday, // monday = 1 tuesday, // tuesday = 2 wednesday, // etc. thursday, friday };
and am declaring a var
enum Days today = sunday;
Can I iterate enum Days, using any loops/ other methods
No, this is not possible. You could provide a global const vector or
array with all the distinct enum values, though. This is what I
usually do.
--
Bob Hairgrove No**********@Ho me.com
On Wed, 11 Aug 2004 12:39:42 +0530, Imran wrote: I have a enum decla like this enum Days // Declare enum type Days { saturday, // saturday = 0 by default sunday = 0, // sunday = 0 as well monday, // monday = 1 tuesday, // tuesday = 2 wednesday, // etc. thursday, friday };
Shared values introduce some complexity in your case, but the code
below is hopefully helpful for you.
and am declaring a var
enum Days today = sunday;
Can I iterate enum Days, using any loops/ other methods
Stroustrup gives an iteration example for enums. This code is based on
that:
template <class Enum>
Enum & enum_increment( Enum & value, Enum begin, Enum end)
{
return value = (value == end) ? begin : Enum(value + 1);
}
enum Days
{
Days_begin,
monday = Days_begin,
tuesday,
wednesday,
thursday,
friday,
saturday,
sunday,
Days_end
};
Days & operator++ (Days & day)
{
return enum_increment( day, Days_begin, Days_end);
}
#include <iostream>
int main()
{
for (Days day = Days_begin; day != Days_end; ++day)
{
std::cout << day << '\n';
}
}
Ali
On Wed, 11 Aug 2004 01:56:58 -0700, Ali Cehreli <ac******@yahoo .com>
wrote: On Wed, 11 Aug 2004 12:39:42 +0530, Imran wrote:
I have a enum decla like this enum Days // Declare enum type Days { saturday, // saturday = 0 by default sunday = 0, // sunday = 0 as well monday, // monday = 1 tuesday, // tuesday = 2 wednesday, // etc. thursday, friday }; Shared values introduce some complexity in your case, but the code below is hopefully helpful for you.
and am declaring a var
enum Days today = sunday;
Can I iterate enum Days, using any loops/ other methods
Stroustrup gives an iteration example for enums. This code is based on that:
template <class Enum> Enum & enum_increment( Enum & value, Enum begin, Enum end) { return value = (value == end) ? begin : Enum(value + 1); }
enum Days { Days_begin,
monday = Days_begin, tuesday, wednesday, thursday, friday, saturday, sunday,
Days_end };
Days & operator++ (Days & day) { return enum_increment( day, Days_begin, Days_end); }
#include <iostream>
int main() { for (Days day = Days_begin; day != Days_end; ++day) { std::cout << day << '\n'; } }
Ali
Interesting ... is that in TC++PL? Never saw it before.
This line bothers me: return value = (value == end) ? begin : Enum(value + 1);
Enum(value + 1) ...
doesn't this give you a diagnostic about "initializi ng enum with int"?
What if value + 1 is not present in the enumeration?
--
Bob Hairgrove No**********@Ho me.com
"Ali Cehreli" <ac******@yahoo .com> wrote in message On Wed, 11 Aug 2004 12:39:42 +0530, Imran wrote:
I have a enum decla like this enum Days // Declare enum type Days { saturday, // saturday = 0 by default sunday = 0, // sunday = 0 as well monday, // monday = 1 tuesday, // tuesday = 2 wednesday, // etc. thursday, friday };
template <class Enum> Enum & enum_increment( Enum & value, Enum begin, Enum end) { return value = (value == end) ? begin : Enum(value + 1); }
Days & operator++ (Days & day) { return enum_increment( day, Days_begin, Days_end); }
Good. But the problem is that in the original code, both Sunday and
Saturday map to zero. In that case I don't think there's any way to iterate
because a value of zero could mean either day, so the next day is either
Sunday or Monday, we don't know which.
So, can you think of other solutions?
"Siemel Naran" <Si*********@RE MOVE.att.net> wrote in message
news:n8******** *************@b gtnsc05-news.ops.worldn et.att.net... "Ali Cehreli" <ac******@yahoo .com> wrote in message On Wed, 11 Aug 2004 12:39:42 +0530, Imran wrote: I have a enum decla like this enum Days // Declare enum type Days { saturday, // saturday = 0 by default sunday = 0, // sunday = 0 as well monday, // monday = 1 tuesday, // tuesday = 2 wednesday, // etc. thursday, friday };
template <class Enum> Enum & enum_increment( Enum & value, Enum begin, Enum end) { return value = (value == end) ? begin : Enum(value + 1); }
Days & operator++ (Days & day) { return enum_increment( day, Days_begin, Days_end); }
Good. But the problem is that in the original code, both Sunday and Saturday map to zero. In that case I don't think there's any way to
iterate because a value of zero could mean either day, so the next day is either Sunday or Monday, we don't know which.
The next day would be Monday, because the next value is (0+1), which is 1,
which is Monday. So your loop would start at Saturday/Sunday (the same!),
followed by Monday, then Tuesday, ...
So, can you think of other solutions?
It depends on what you want to do.
If you want to loop over the enumeration, and have that loop include both
Saturday and Sunday as *different* loop values (i.e., you want them to be
handled in seperate passes through the loop), then it's simply not possible,
because you've defined them as the *same* value. Saturday and Sunday are
identical.
You could loop, using integers that start at Sunday (or Saturday) and end at
(after) Friday, but when that integer is 0, you won't be able to tell if
it's Saturday or Sunday, because it will simply be zero, which is valid for
both Saturday and Sunday. So that will work only if you can live with
treating day 0 as one day, ignoring whether it was intended to be Saturday
or Sunday. (Call it "Weekend"? :-))
If you need Saturday and Sunday to be handled in different passes through
the loop, then they absolutely *must* have different values. Perhaps you
could define a separate enumeration where they have different values?
One other idea: don't loop. You have only seven cases, right? Why not
handle them explicitly, instead of via a loop? So instead of something like
for (i = Saturday; i <= Friday; ++i)
DoOneDay(i);
write
DoSaturday();
DoSunday();
DoMonday();
DoTuesday();
etc.
-Howard
On Wed, 11 Aug 2004 03:58:42 -0700, Bob Hairgrove wrote: On Wed, 11 Aug 2004 01:56:58 -0700, Ali Cehreli <ac******@yahoo .com> wrote:
Can I iterate enum Days, using any loops/ other methods
Stroustrup gives an iteration example for enums. This code is based on that:
template <class Enum> Enum & enum_increment( Enum & value, Enum begin, Enum end) { return value = (value == end) ? begin : Enum(value + 1); }
I had used this utility template in a personal fun project before. I
used it for the implementation of operator++ of more than one enum
type.
enum Days { Days_begin,
monday = Days_begin, tuesday, wednesday, thursday, friday, saturday, sunday,
Days_end };
Days & operator++ (Days & day) { return enum_increment( day, Days_begin, Days_end); }
#include <iostream>
int main() { for (Days day = Days_begin; day != Days_end; ++day) { std::cout << day << '\n'; } }
Interesting ... is that in TC++PL? Never saw it before.
Yes, in TC++PL section 11.2.3, Operators and User-Defined Types.
He uses the days of the week as well :)
<quote>
enum Day { sun, mon, tue, wed, thu, fri, sat };
Day& operator++(Day& d)
{
return (d = (sat==d) ? sun : Day(d+1);
}
</quote>
This line bothers me: return value = (value == end) ? begin : Enum(value + 1); Enum(value + 1) ... doesn't this give you a diagnostic about "initializi ng enum with int"?
The explicit construction of the enum is valid. Using 'value + 1'
alone would be an error.
What if value + 1 is not present in the enumeration?
I think you missed the (value == end) check. Incrementing the last
value wraps the variable back to the beginning. This behavior is
arguable but makes the enum act like an int...
Ali
On Wed, 11 Aug 2004 11:29:11 -0700, Ali Cehreli <ac******@yahoo .com>
wrote:
[snip] This line bothers me: return value = (value == end) ? begin : Enum(value + 1);
Enum(value + 1) ... doesn't this give you a diagnostic about "initializi ng enum with int"?
The explicit construction of the enum is valid. Using 'value + 1' alone would be an error.
What if value + 1 is not present in the enumeration?
I think you missed the (value == end) check. Incrementing the last value wraps the variable back to the beginning. This behavior is arguable but makes the enum act like an int...
But enums don't have to have contiguous values. This shouldn't work
if, for example, values 5 and 7 are present in an enum, and 6 is
missing ... or does "value+1" somehow evaluate to 7 when value==5??
--
Bob Hairgrove No**********@Ho me.com
"Bob Hairgrove" <in*****@bigfoo t.com> wrote in message
news:n4******** *************** *********@4ax.c om...
<snip> But enums don't have to have contiguous values. This shouldn't work if, for example, values 5 and 7 are present in an enum, and 6 is missing ... or does "value+1" somehow evaluate to 7 when value==5??
The code was written with contiguous values in mind. It will not work for
non-contiguous values, and I don't know of any way that one could possibly
iterate over an enum with non-contiguous values without doing more work.
--
David Hilsee This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Mattias Brändström |
last post by:
Hi!
Suppose I want to do some thing like this:
enum X { A, B, C, D };
int main()
{
X a1 = { A, B, C, D };
int size_a = sizeof(a1) / sizeof(X);
|
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: Jamie Winder via .NET 247 |
last post by:
Is it possible to iterate through all of the possible values of an enumeration? (with foreach, maybe?)
What I need to do is fill a ComboBox with all possible values for an enumeration.
e.g
foreach ( in )
{
comboBox1.Items.Add (value.ToString ("G");
}
|
by: ssg31415926 |
last post by:
I have an abstract base class with an enum which is passed into a
method. I want my derived classes to be able to add new values to the
enum without having to redefine it and I want to be able to pass them
into the method defined into the base class. Is there a way to do
this.
E.g.
abstract public class BaseWidget
{
|
by: Simon Hart |
last post by:
Is this possible?
I have a load of items defined in a enum. I simply want to check if a string
variable if defined in anyone of those enum items. Don't want to have to use
if statement.
Regards
Simon.
| |
by: sven.suursoho |
last post by:
Does messing with signal handlers and longjmp affect Python
interpreter?
I'm trying to find solution for problem, described in
http://groups.google.com/group/comp.lang.python/browse_thread/thread/98cbae94ca4beefb/9d4d96fd0dd9fbc3
and came up with test application. It works well but i'm not sure it is
ok for long-running python interpreter?
#include <Python.h>
#include <signal.h>
|
by: Matthias Langbein |
last post by:
Hi all,
i want to save the position of my Addin-Toolbar to the registry. It's
easy to store the position as a string with MsoBarPosition.toString().
But when I read the key, I get it as a string and I want to assign the
value to the MsoBarPosition. Is there a better way than iterating over
all possibilities in a CASE call?
Thx, Langi
|
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 snippet that I wrote today that gives me
an instant implementation of the pattern. I could easily just always
use such an implementation instead of a standard enum, so I wanted to
know what you experts all thought.
Is there a case for standard enums?
|
by: Gary |
last post by:
I have a status text on my status bar which simply lists the logged in
user name.
the code that puts text into this is:
stUserName.Text = "Currently logged in user: " +
System.Environment.UserName;
However our login names here are firstname, surname initial, like so: -
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |