C++ 将 switch 语句情况组合在一起?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4494170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Grouping switch statement cases together?
提问by Josh Lake
I may be over looking something but is there a simple way in C++ to group cases together instead of writing them out individually? I remember in basic I could just do:
我可能会看一些东西,但是在 C++ 中有没有一种简单的方法可以将案例组合在一起而不是单独写出来?我记得基本我可以这样做:
SELECT CASE Answer
CASE 1, 2, 3, 4
Example in C++ (For those that need it):
C++ 中的示例(对于那些需要它的人):
#include <iostream.h>
#include <stdio.h>
int main()
{
int Answer;
cout << "How many cars do you have?";
cin >> Answer;
switch (Answer)
{
case 1:
case 2:
case 3:
case 4:
cout << "You need more cars. ";
break;
case 5:
case 6:
case 7:
case 8:
cout << "Now you need a house. ";
break;
default:
cout << "What are you? A peace-loving hippie freak? ";
}
cout << "\nPress ENTER to continue... " << endl;
getchar();
return 0;
}
采纳答案by moinudin
No, but you can with an if
-else if
-else
chain which achieves the same result:
不,但您可以使用if
- else if
-else
链实现相同的结果:
if (answer >= 1 && answer <= 4)
cout << "You need more cars.";
else if (answer <= 8)
cout << "Now you need a house.";
else
cout << "What are you? A peace-loving hippie freak?";
You may also want to handle the case of 0 cars and then also the unexpected case of a negative number of cars probably by throwing an exception.
您可能还想处理 0 辆汽车的情况,然后可能通过抛出异常来处理汽车数量为负的意外情况。
PS: I've renamed Answer
to answer
as it's considered bad style to start variables with an uppercase letter.
PS:我已重命名为Answer
,answer
因为以大写字母开头的变量被认为是不好的风格。
As a side note, scripting languages such as Python allow for the nice if answer in [1, 2, 3, 4]
syntax which is a flexible way of achieving what you want.
作为旁注,诸如 Python 之类的脚本语言允许使用漂亮的if answer in [1, 2, 3, 4]
语法,这是一种实现您想要的灵活方式。
回答by Leo Davidson
AFAIK all you can do is omit the returns to make things more compact in C++:
AFAIK 你所能做的就是省略返回值以使 C++ 中的事情更紧凑:
switch(Answer)
{
case 1: case 2: case 3: case 4:
cout << "You need more cars.";
break;
...
}
(You could remove the other returns as well, of course.)
(当然,您也可以删除其他回报。)
回答by Ankit Patel
Sure you can.
你当然可以。
You can use case x ... y for the range
您可以使用 case x ... y 作为范围
Example:
例子:
#include <iostream.h>
#include <stdio.h>
int main()
{
int Answer;
cout << "How many cars do you have?";
cin >> Answer;
switch (Answer)
{
case 1 ... 4:
cout << "You need more cars. ";
break;
case 5 ... 8:
cout << "Now you need a house. ";
break;
default:
cout << "What are you? A peace-loving hippie freak? ";
}
cout << "\nPress ENTER to continue... " << endl;
getchar();
return 0;
}
Make sure you have "-std=c++0x" flag enabled within your compiler
确保在编译器中启用了“-std=c++0x”标志
回答by Dialecticus
You can't remove keyword case
. But your example can be written shorter like this:
您不能删除关键字case
。但是你的例子可以写得更短:
switch ((Answer - 1) / 4)
{
case 0:
cout << "You need more cars.";
break;
case 1:
cout << "Now you need a house.";
break;
default:
cout << "What are you? A peace-loving hippie freak?";
}
回答by aschepler
Your example is as concise as it gets with the switch
construct.
您的示例与switch
构造一样简洁。
回答by aschepler
gcc has a so-called "case range" extension:
gcc 有一个所谓的“case range”扩展:
http://gcc.gnu.org/onlinedocs/gcc-4.2.4/gcc/Case-Ranges.html#Case-Ranges
http://gcc.gnu.org/onlinedocs/gcc-4.2.4/gcc/Case-Ranges.html#Case-Ranges
I used to use this when I was only using gcc. Not much to say about it really -- it does sort of what you want, though only for ranges of values.
当我只使用gcc时,我曾经使用过这个。关于它真的没什么好说的——它确实是你想要的,尽管只适用于值的范围。
The biggest problem with this is that only gcc supports it; this may or may not be a problem for you.
最大的问题是只有 gcc 支持它;这对您来说可能是也可能不是问题。
(I suspect that for your example an if
statement would be a more natural fit.)
(我怀疑对于您的示例,if
语句会更自然。)
回答by burakim
You can use like this:
你可以这样使用:
case 4: case 2:
{
//code ...
}
For use 4 or 2 switch case.
用于 4 或 2 个开关盒。
回答by SirGuy
If you're willing to go the way of the preprocessor abuse, Boost.Preprocessor
can help you.
如果你愿意走预处理器滥用的道路,Boost.Preprocessor
可以帮助你。
#include <boost/preprocessor/seq/for_each.hpp>
#define CASE_case(ign, ign2, n) case n:
#define CASES(seq) \
BOOST_PP_SEQ_FOR_EACH(CASE_case, ~, seq)
CASES((1)(3)(15)(13))
Running this through gcc
with -E -P
to only run the preprocessor, the expansion of CASES
gives:
通过运行这个gcc
与-E -P
只运行预处理器的扩展CASES
提供了:
case 1: case 3: case 15: case 13:
Note that this probably wouldn't pass a code review (wouldn't where I work!) so I recommend it be constrained to personal use.
请注意,这可能不会通过代码(不会在我工作的地方通过!)所以我建议它仅限于个人使用。
It should also be possible to create a CASE_RANGE(1,5)
macro to expand to
也应该可以创建一个CASE_RANGE(1,5)
宏来扩展到
case 1: case 2: case 3: case 4: case 5:
for you as well.
也适合你。
回答by Mitro
No, unless you want to break compatibility andyour compiler supports it.
不,除非您想破坏兼容性并且您的编译器支持它。