开关优化 [英] Switch Optimization

查看:97
本文介绍了开关优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在将一些代码转换为C ++。我正在尝试使用Switch

函数来比较结果。是否可以使用开关来评估

''> 0'',''< 0'',0?


示例


开关(结果)

{

case(> 0):

case(< 0) ):

默认:

}


我知道这可以用其他语言完成,但我有所有文档表明你必须评估const表达式(单个值)。

我目前正在使用和If - Else If - Else结构。有没有人有一个

高效的工作我可以坚持下去?


Thanx

Mike

I''ve been converting some code to C++. I''m trying to use the Switch
function to compare a result. Is it possible to use switch to evaluate
''>0'', ''<0'', 0?

Example

switch (result)
{
case (>0):
case (<0):
default:
}

I know this can be done in other languages but all the documentation I have
found in C++ show that you must evaluate const expressions (single values).
I am currently using and If - Else If - Else structure. Does anyone have an
efficient work around I can persue?

Thanx
Mike

推荐答案

Mike和Jo写道:
Mike and Jo wrote:
我一直在将一些代码转换为C ++。我正在尝试使用Switch
功能来比较结果。是否可以使用开关来评估
''> 0'',''< 0'',0?

示例

切换(结果)
{
案例(> 0):
案例(< 0):
默认:
}

我知道这个可以用其他语言完成,但我在C ++中找到的所有文档都表明你必须评估const表达式(单个值)。
我目前正在使用和If - Else If - Else结构。有没有人能够有效地完成工作?
I''ve been converting some code to C++. I''m trying to use the Switch
function to compare a result. Is it possible to use switch to evaluate
''>0'', ''<0'', 0?

Example

switch (result)
{
case (>0):
case (<0):
default:
}

I know this can be done in other languages but all the documentation I have
found in C++ show that you must evaluate const expressions (single values).
I am currently using and If - Else If - Else structure. Does anyone have an
efficient work around I can persue?




" if(){} else if(){} else {}"是非常快,可能比

或等于switch(结果== 0?(结果> 0?GT:LT):EQL)


如果你知道你可以做的第一个数据,那么第一个如果成功的大部分时间,但即使在那里,我只是花了更多的时间来输入这个回复

你可能会保存。



"if () {} else if () {} else {}" is pretty fast and likely faster than
or equal to "switch ( result == 0 ? ( result > 0 ? GT : LT ) : EQL )"

If you know the data you could make it so the first if succeeded most of
the time but even there, I''ve just spent more time typing this response
that you will likely ever save.


2003年10月15日星期三20:20:34 -0400,Mike和Jo

< mi ******* @ sympatico.ca>在comp.lang.c ++中写道:
On Wed, 15 Oct 2003 20:20:34 -0400, "Mike and Jo"
<mi*******@sympatico.ca> wrote in comp.lang.c++:
我一直在将一些代码转换为C ++。我正在尝试使用Switch
功能来比较结果。是否可以使用开关来评估
''> 0'',''< 0'',0?


No.

示例

开关(结果)
{
情况(> 0) :
案例(< 0):
默认:
}

我知道这可以用其他语言完成,但我有所有文档
在C ++中发现,你必须评估const表达式(单个值)。
我目前正在使用和If - Else If - Else结构。有没有人有可靠的工作?

Thanx
Mike
I''ve been converting some code to C++. I''m trying to use the Switch
function to compare a result. Is it possible to use switch to evaluate
''>0'', ''<0'', 0?
No.
Example

switch (result)
{
case (>0):
case (<0):
default:
}

I know this can be done in other languages but all the documentation I have
found in C++ show that you must evaluate const expressions (single values).
I am currently using and If - Else If - Else structure. Does anyone have an
efficient work around I can persue?

Thanx
Mike




案例标签switch语句必须是编译时常量

表达式,计算整数类型值(意味着任何

整数类型,而不仅仅是int)。

-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html

comp.lang.c ++ http:// www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++ ftp://snurse-l.org/pub/acllc-c++/faq



The case labels in a switch statement must be compile-time constant
expressions that evaluate to an integer type value (meaning any
integer type, not just int).

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq


" Mike and Jo" < MI ******* @ sympatico.ca>在消息中写道

news:iv ******************* @ news20.bellglobal.com ..。
"Mike and Jo" <mi*******@sympatico.ca> wrote in message
news:iv*******************@news20.bellglobal.com.. .
我一直在将一些代码转换为C ++。我正在尝试使用Switch
功能来比较结果。是否可以使用开关来评估
''> 0'',''< 0'',0?


编号另请注意,切换语句在C ++中与在C中的工作方式相同。


'' switch''用于将单个

(变量)值与一组已知值进行比较。


示例

switch(result)
{
case(> 0):
0不是值。
结果> 0有一个值大小写(< 0):


< 0不是一个值。

结果< 0有一个值。

默认值:
}


如果你坚持:


开关(结果){

案例0:

/ *结果== 0 * /

休息;

默认:

/ *结果!= 0 * /

开关(结果> 0){

案例1:

/ *结果> 0 * /

休息;

默认:

/ *结果< 0 * /

休息;

}

}

/ *总计15行* /


但那只是愚蠢的。 :-)


if(result)

; / *结果> 0或< 0 * /

其他

if(结果> 0)

; / *结果> 0 * /

其他

; / *结果== 0 * /


/ *总共7行。 * /


此外,输入

比''''和''其他'更短,''切换'',''案例n:''和''默认'',你不觉得吗? :-)

我想你想用一个工具来完成一个任务

它不适合哪个更好的一个

已经存在。

我知道这可以用其他语言完成


IMO没理由想用C ++做这个br />
但我在C ++中找到的所有文档都表明你必须评估const表达式(单个
值)。


那个'是的。

我目前正在使用和If - Else If - Else结构。有没有人有
一个有效的工作我可以坚持下去?
I''ve been converting some code to C++. I''m trying to use the Switch
function to compare a result. Is it possible to use switch to evaluate
''>0'', ''<0'', 0?
No. Also note that switch statements work the same
in C++ as in C.

''switch'' is intended for use to compare a single
(variable) value against a set of known values.

Example

switch (result)
{
case (>0): 0 is not a value. result > 0 has a value case (<0):
<0 is not a value.
result < 0 has a value.
default:
}
If you insist:

switch(result) {
case 0:
/* result == 0 */
break;
default:
/* result != 0 */
switch(result > 0) {
case 1:
/* result > 0 */
break;
default:
/* result < 0 */
break;
}
}
/* total of 15 lines */

But that''s just silly. :-)

if(result)
; /* result > 0 or < 0 */
else
if(result > 0)
; /* result > 0 */
else
; /* result == 0 */

/* total of 7 lines. */

Also, the words ''if'' and ''else'' are shorter to type
than ''switch'', ''case n:'', and ''default'', don''t you think? :-)
I think you''re wanting to use a tool for a task
for which it is not suited and for which a better one
already exists.
I know this can be done in other languages
IMO that''s no reason to want to do it in C++
but all the documentation I have
found in C++ show that you must evaluate const expressions (single values).

That''s right.
I am currently using and If - Else If - Else structure. Does anyone have an efficient work around I can persue?




if / else有什么问题?


-Mike



What''s wrong with if/else?

-Mike


这篇关于开关优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆