C++ “OR” operator

后端 未结 9 1494
无人及你
无人及你 2021-01-19 03:37

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.

相关标签:
9条回答
  • 2021-01-19 03:46

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

    0 讨论(0)
  • 2021-01-19 03:46

    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.

    0 讨论(0)
  • 2021-01-19 03:47

    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;

    0 讨论(0)
  • 2021-01-19 03:55

    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;
    }
    
    0 讨论(0)
  • 2021-01-19 04:03

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

    0 讨论(0)
  • 2021-01-19 04:07

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

    0 讨论(0)
提交回复
热议问题