本文介绍了比较多个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的c ++课程开发一个项目而我遇到了麻烦

使用or运算符将一个字符串与另外两个字符串进行比较。它看起来像这样:
if(answer3 == answer1 || answer2)

我需要通过另一个命令吗?或者我应该以完全不同的方式做一个



任何建议都会非常感激。

I''m working on a project for my c++ class and I am having trouble
comparing one string to two others using the or operator. It looks
something like this:
if(answer3 == answer1 || answer2)
Is there another command I need to through in there or should I do it a
completely different way?
Any suggestions would be most appreciated.

推荐答案




if(answer3 == answer1 || answer3 == answer2)

-

John Carson



if(answer3 == answer1 || answer3 == answer2)
--
John Carson





你不能这样做,因为(||)在==之前进行评估。


你必须使用两个比较(每个答案一个)和/或结果。


-

Ian Collins。



You can''t do that because the or (||) is evaluated before the ==.

You have to use two comparisons (one for each answer) and or the result.

--
Ian Collins.




if(answer3 == answer1 || answer3 == answer2)

-



if(answer3 == answer1 || answer3 == answer2)
--
John Carson




||是一个捷径运算符,因此如果第一个表达式为真,那么第二个表达式就不会被计算出来。所以,如果(answer3 ==

answer1)证明是真的那么(answer3 == answer2)就不会执行
了。不确定你是否想要这样。


另外,我建议你使用strcmp进行字符串比较而不是

==运算符除非当然,如果你超载了==

运算符。



The || is a short cut operator hence if the first expression is true
the second expression would not be calculated. So, if (answer3 ==
answer1) turns out to be true the (answer3 == answer2) would not be
carried out. Not sure if you want it that way or not.

Also, I would suggest you to use strcmp for string comparison instead
of the == operator unless of-course if you have overloaded the ==
operator.


这篇关于比较多个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 11:22