重载增量运算符、循环和边缘情况
我有一个枚举,看起来像这样:
enum Suit {Clubs, Diamonds, Hearts, Spades};
我想重载增量运算符,这样我就可以轻松地循环这四个家伙。
当变量是梅花、钻石或红心时,没有问题。这是黑桃条件给我带来了一些麻烦。
我的第一直觉是定义它,以便当变量是黑桃时,增量将其设置为等于梅花。问题是这似乎使得无法循环遍历枚举中的 4 个值。
如果我做了类似的事情
for(Suit i=Clubs;i<Spades;++i)
{cout<<i<<endl;}
,那么我的输出只会流向红心。
如果我
for(suit i=Clubs;i<=Spades;++i)
{cout<<i<<endl;}
这样做,那么我的输出就会永远循环!
所以,我显然可以想到一些解决方法......我只是不确定惯用的 C++ 的作用是什么。
我是否应该重新定义增量,以便尝试增加 Spade 会得到 Spade?或者可能会抛出异常?
重申一下:我绝对可以想出一些古怪的方法来解决这个问题。我只是希望有经验的程序员的指导告诉我他们认为解决问题的最“正常”方法是什么。
I have an enum, that looks like this:
enum Suit {Clubs, Diamonds, Hearts, Spades};
I want to overload the increment operator, so I can easily loop over these four dudes.
When the variable is Clubs, Diamonds, or Hearts there no issue. Its the Spades condition that is giving me a little trouble.
My first instinct was to define it so that when the variable is spades, incrementation sets it equal to Clubs. The problem is that this seems to make it impossible to loop over the 4 values in the enum.
If I do something like
for(Suit i=Clubs;i<Spades;++i)
{cout<<i<<endl;}
then my output only goes to Hearts.
If i do
for(suit i=Clubs;i<=Spades;++i)
{cout<<i<<endl;}
then my output just loops forever!
So, I can obviously think of a few workarounds for this... I'm just not sure what the idiomatic C++ thing do to.
Should I redefine incrementation so that attempting to increment a Spade results in a Spade? or maybe throws an exception?
To reiterate: I can definitely think of a few hacky ways to fix this issue. I just want the guidance of experienced programmers to tell me what they think would be the most "normal" way to solve the problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以为开始和终止条件添加
enum
值,以及不循环回到开头的++
的替代方案。由于
for
和while
循环在执行之前总是检查条件,因此如果没有额外的变量或流程控制,就无法在循环范围的中间结束它们的执行。在这种情况下,do while
循环效果最好。在这种情况下,您不需要
FirstSuit
和AllSuits
。You could add
enum
values for start and termination conditions, and an alternative to++
which doesn't cycle back to the beginning.Since
for
andwhile
loops always check the condition before executing, there is no way to end their execution in the middle of a cyclic range without additional variables or flow control. Ado while
loop works best in this case.In this case, you don't need
FirstSuit
andAllSuits
.正如评论中所建议的,将您的枚举定义为:
然后您的循环将变为:
As suggested in a comment, what about just defining your enum as:
Then your loop becomes:
也许
还有一些不惯用的地方(我必须知道梅花是第一花色)。
为什么不直接使用隐式转换为 int 呢?
Perhaps
There's still something unidiomatic (I have to know that Clubs is the first suit).
Why not just use the implicit conversion to int?
由于
enum
值衰减为(无符号)整数,您可以使用moduluo算术,然后转换回enum
:建议:把它做成一个类。通过类,您还可以添加“打印”和“输入”方法。
Since
enum
values decay into (unsigned) integers, you could use moduluo arithmetic and then cast back to anenum
:Suggestion: Make this into a class. With a class you can also add "print" and "input" methods.