C++“或”运算符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5058664/
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
C++ "OR" operator
提问by Jaanus
can this be done somehow?
这可以以某种方式完成吗?
if((a || b) == 0) return 1;
return 0;
so its like...if a OR b equals zero, then...but it is not working for me. my real code is:
所以它就像......如果 a OR b 等于零,那么......但它对我不起作用。我真正的代码是:
bool Circle2::contains(Line2 l) {
if((p1.distanceFrom(l.p1) || p1.distanceFrom(l.p2)) <= r) {
return 1;
}
return 0;
}
回答by CodesInChaos
You need to write the full expression:
你需要写出完整的表达式:
(a==0)||(b==0)
And in the second code:
在第二个代码中:
if((p1.distanceFrom(l.p1)<= r) || (p1.distanceFrom(l.p2)<=r) )
return 1;
If you do ((a || b) == 0)
this means "Is the logical or of a
and b
equal to 0. And that's not what you want here.
如果你这样做,((a || b) == 0)
这意味着“是逻辑 ora
并且b
等于 0。这不是你想要的。
And as a side note: the if (BooleanExpression)return true; else return false
pattern can be shortened to return BooleanExpression;
作为旁注:if (BooleanExpression)return true; else return false
模式可以缩短为return BooleanExpression;
回答by tpdi
The C++ language specifies that the operands of ||
("or") be boolean expressions.
C++ 语言指定||
("or")的操作数是布尔表达式。
If p1.distanceFrom(l.p1)
is not boolean (that is, if distanceFrom
returns int, or double, or some numeric class type), the compiler will attempt to convert it to boolean.
如果p1.distanceFrom(l.p1)
不是布尔值(即,如果distanceFrom
返回 int、double 或某些数字类类型),编译器将尝试将其转换为 boolean。
For built in numeric type, the conversion is: non-zero converts to true, zero converts to false. If the type of p1.distanceFrom(l.p1)
is of class type Foo
, the compiler will call one (and only one) user defined conversion, e.g., Foo::operator bool()
, to convert the expression's value to bool.
对于内置数字类型,转换为:非零转换为真,零转换为假。如果 的类型p1.distanceFrom(l.p1)
是类 type Foo
,编译器将调用一个(并且只有一个)用户定义的转换,例如,Foo::operator bool()
将表达式的值转换为 bool。
回答by Rao
You have to specify the condition separately each time:
您必须每次单独指定条件:
if (a == 0) || (b == 0))
bla bla;
When you do
当你做
if ((a || b) == 0)
bla bla;
it has a different meaning: (a || b) means "if either a or b is non-zero (ie. true), then the result of this expression is true". So when you do (a||b) == 0, you are checking if the result of the previously explained expression is equal to zero (or false).
它有不同的含义:(a || b) 表示“如果 a 或 b 非零(即真),则该表达式的结果为真”。因此,当您执行 (a||b) == 0 时,您正在检查先前解释的表达式的结果是否等于零(或假)。
回答by Jim Lewis
I think you really want something like this:
我想你真的想要这样的东西:
bool Circle2::contains(Line2 l) {
if((p1.distanceFrom(l.p1) <= r) || (p1.distanceFrom(l.p2) <= r)) return 1;
return 0;
}
回答by Marlon
Fun with templates:
有趣的模板:
template <typename T>
struct or_t
{
or_t(const T& a, const T& b) : value1(a), value2(b)
{
}
bool operator==(const T& c)
{
return value1 == c || value2 == c;
}
private:
const T& value1;
const T& value2;
};
template <typename T>
or_t<T> or(const T& a, const T& b)
{
return or_t<T>(a, b);
}
In use:
正在使用:
int main(int argc, char** argv)
{
int a = 7;
int b = 9;
if (or(a, b) == 7)
{
}
return 0;
}
It performs the same comparison you would normally do, though, but at your convenience.
不过,它执行的比较与您通常会执行的比较相同,但在您方便时。
回答by user unknown
If you have lot of that code, you may consider a helping method:
如果你有很多这样的代码,你可以考虑一个帮助方法:
bool distanceLE (Point p1, Point p2, double threshold) {
return (p1.distanceFrom (p2) <= threshold)
}
bool Circle2::contains (Line2 l) {
return distanceLE (p1, l.p1, r) && distanceLE (p1, l.p2, r);
}
If you sometimes have <, sometimes <=, >, >= and so on, maybe you should pass the operator too, in form of a function.
如果你有时有 <,有时 <=,>,>= 等等,也许你也应该以函数的形式传递运算符。
In some cases your intentions by writing this:
在某些情况下,您的意图是这样写:
if ((a || b) == 0) return 1;
return 0;
could be expressed with an bitwise-or:
可以用按位或表示:
if ((a | b) == 0) return 1;
return 0;
and simplified to
并简化为
return ! (a | b);
But read up on bitwise operations and test it carefully. I use them rarely and especially I didn't use C++ for some time.
但请仔细阅读按位运算并仔细测试。我很少使用它们,尤其是我有一段时间没有使用 C++。
Note, that you inverted the meaning between your examples 1 and 2, returning true and false in the opposite way.
请注意,您颠倒了示例 1 和示例 2 之间的含义,以相反的方式返回 true 和 false。
And bitwise less-equal doesn't make any sense, of course. :)
当然,按位不相等没有任何意义。:)
回答by KyleWpppd
C++ isn't that smart. You have to do each comparison manually.
C++ 没有那么聪明。您必须手动进行每次比较。
bool Circle2::contains(Line2 l) {
if((p1.distanceFrom(l.p1) <= r) || (p1.distanceFrom(l.p2) <= r)) return 1;
return 0;
}
回答by Cat Plus Plus
C++ doesn't support any construct like that. Use if (a == 0 || b == 0)
.
C++ 不支持任何类似的构造。使用if (a == 0 || b == 0)
.
回答by helium
Your condition should be (a == 0 || b == 0)
or (p1.distanceFrom(l.p1) <= r || p1.distanceFrom(l.p2)) <= r)
你的情况应该是(a == 0 || b == 0)
或(p1.distanceFrom(l.p1) <= r || p1.distanceFrom(l.p2)) <= r)