C++ “或”操作员

发布于 2024-10-18 01:59:43 字数 305 浏览 5 评论 0原文

这可以以某种方式完成吗?

if((a || b) == 0) return 1;
return 0;

所以它就像...如果 a OR b 等于 0,那么...但它对我不起作用。 我真正的代码是:

bool Circle2::contains(Line2 l) {
    if((p1.distanceFrom(l.p1) || p1.distanceFrom(l.p2)) <= r) {
        return 1;
    }
    return 0;
}

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:

bool Circle2::contains(Line2 l) {
    if((p1.distanceFrom(l.p1) || p1.distanceFrom(l.p2)) <= r) {
        return 1;
    }
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

硪扪都還晓 2024-10-25 01:59:44

您需要编写完整的表达式:

(a==0)||(b==0)

在第二个代码中:

if((p1.distanceFrom(l.p1)<= r) || (p1.distanceFrom(l.p2)<=r) )
    return 1;

如果您执行 ((a || b) == 0) 这意味着“是 ab 等于 0。这不是您想要的。

附带说明:if (BooleanExpression)return true; 模式可以缩短。 返回布尔表达式;

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.

And as a side note: the if (BooleanExpression)return true; else return false pattern can be shortened to return BooleanExpression;

往事随风而去 2024-10-25 01:59:44

每次都必须单独指定条件:

if (a == 0) || (b == 0))
    bla bla;

当您这样做时

if ((a || b) == 0)
    bla bla;

,它具有不同的含义: (a || b) 表示“如果 a 或 b 为非零(即 true),则此表达式的结果为 true ”。
因此,当您执行 (a||b) == 0 时,您正在检查先前解释的表达式的结果是否等于零(或假)。

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).

别想她 2024-10-25 01:59:44

C++ 语言规定||(“或”)的操作数是布尔表达式。

如果 p1.distanceFrom(l.p1) 不是布尔值(即,如果 distanceFrom 返回 int、double 或某些数字类类型),编译器将尝试将其转换为布尔值。

对于内置数字类型,转换为:非零转换为 true,零转换为 false。如果p1.distanceFrom(l.p1)的类型是类类型Foo,编译器将调用一个(且仅一个)用户定义的转换,例如,Foo::operator bool(),将表达式的值转换为 bool。

The C++ language specifies that the operands of || ("or") be boolean expressions.

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.

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.

与之呼应 2024-10-25 01:59:44

模板的乐趣:

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);
}

使用中:

int main(int argc, char** argv)
{
  int a = 7;
  int b = 9;

  if (or(a, b) == 7)
  {
  }

  return 0;
}

它执行的比较与您通常执行的比较相同,但只是在您方便的时候进行。

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.

衣神在巴黎 2024-10-25 01:59:44

我想你真的想要这样的东西:

bool Circle2::contains(Line2 l) {
if((p1.distanceFrom(l.p1) <= r) || (p1.distanceFrom(l.p2) <= r)) return 1;
    return 0;
}

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;
}
屋顶上的小猫咪 2024-10-25 01:59:44

如果你有很多这样的代码,你可以考虑一个帮助方法:

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 ((a || b) == 0) return 1;
return 0;

可以用按位或:来表达

if ((a | b) == 0) return 1;
return 0;

,并简化为

return  ! (a | b);

但是阅读按位运算并仔细测试它。我很少使用它们,尤其是我有一段时间没有使用 C++ 了。

请注意,您颠倒了示例 1 和 2 之间的含义,以相反的方式返回 true 和 false。

当然,按位不相等没有任何意义。 :)

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.

Note, that you inverted the meaning between your examples 1 and 2, returning true and false in the opposite way.

And bitwise less-equal doesn't make any sense, of course. :)

奶茶白久 2024-10-25 01:59:44

C++ 不支持任何类似的构造。使用if (a == 0 || b == 0)

C++ doesn't support any construct like that. Use if (a == 0 || b == 0).

揪着可爱 2024-10-25 01:59:44

您的条件应该是 (a == 0 || b == 0)(p1.distanceFrom(l.p1) <= r || p1.distanceFrom(l.p2) ) <= r)

Your condition should be (a == 0 || b == 0) or (p1.distanceFrom(l.p1) <= r || p1.distanceFrom(l.p2)) <= r)

吖咩 2024-10-25 01:59:44

C++ 没那么聪明。您必须手动进行每个比较。

bool Circle2::contains(Line2 l) {
if((p1.distanceFrom(l.p1) <= r) || (p1.distanceFrom(l.p2) <= r)) return 1;
    return 0;
}

C++ isn't that smart. You have to do each comparison manually.

bool Circle2::contains(Line2 l) {
if((p1.distanceFrom(l.p1) <= r) || (p1.distanceFrom(l.p2) <= r)) return 1;
    return 0;
}
绿光 2024-10-25 01:59:44

正如您所知,在 C++ 中,0 为 false,1 为 true。考虑到这一点,回答你的第一个问题(代码 1):

return !(a && b);

如果其中一个布尔值为假,则 a && b 为 false,因此 !(a && b) 为 true,即 1,否则 a 和 b 均为 true,因此 a & ;& b 为 true,因此 !(a && b) 为 false,即 0。您也可以执行(代码 2),

return !a || !b;

但这最多执行三个逻辑运算,而第一个最多执行两个逻辑运算,数量较少。这两个表达式的等价性是德摩根定律之一。

要修复第二个代码(代码 3):

bool Circle2::contains(Line2 l) {
    return p1.distanceFrom(l.p1) <= r || p1.distanceFrom(l.p2) <= r;
}

如果您想避免双重 <= r(代码 4):

bool Circle2::contains(Line2 l) {
    for (auto p : { l.p1, l.p2 }) {
        if (p1.distanceFrom(p) <= r) {
            return true;
        }
    }
    return false;
};

for 循环初始化两个值的匿名数组、l.p1l.p2,并检查每一个,如果找到满意的点,则立即返回 true。这意味着它会短路,就像 || 一样。仅当发现两个点都不满足时才返回false。我建议使用类似 Code 3 的内容,因为要检查的两个点足够简单,只需使用简单的 || 表达式即可。在您必须检查很多点的替代场景中,类似代码 4 的内容会很有用。

As you seem to know, in C++, 0 is false and 1 is true. With that in mind, to answer your first question (Code 1):

return !(a && b);

If one of the bools is false then a && b is false so !(a && b) is true i.e., 1, otherwise both a and b are true so a && b is true so !(a && b) is false i.e., 0. You can also do (Code 2)

return !a || !b;

but this performs up to three logical operations whereas the first one performs up to two, which is less. The equivalence of these two expressions is one of De Morgan's laws.

To fix your second code (Code 3):

bool Circle2::contains(Line2 l) {
    return p1.distanceFrom(l.p1) <= r || p1.distanceFrom(l.p2) <= r;
}

If you want to avoid the double <= r (Code 4):

bool Circle2::contains(Line2 l) {
    for (auto p : { l.p1, l.p2 }) {
        if (p1.distanceFrom(p) <= r) {
            return true;
        }
    }
    return false;
};

The for loop initializes an anonymous array of two values, l.p1 and l.p2, and checks each one and returns true immediately if it finds a satisfying point. That means that it short-circuits, like ||. false is only returned after both points were found not to satisfy. I would recommend using something like Code 3 because two points to check is simple enough to do with a simple || expression. In an alternate scenario where you had to check a lot of points, something like Code 4 would be useful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文