简体   繁体   English

在评估中使用多个“大于/小于”比较器是否合适?

[英]Is it proper to use multiple 'greater-than / less-than' comparators in an evaluation?

I was trying to see if a variable was within a range by using multiple greater-than (>) 's and less-than (<) 's in one evaluation. 我试图通过在一个评估中使用多个greater-than (>)less-than (<)来查看变量是否在范围内。 I was unsure whether this works or not though. 我不确定这是否行得通。 It seems than using multiple =='s is illigal , but is it okay with < and >'s? 似乎使用多个==是非法的 ,但使用<和>可以吗?

Here's an example of what I'm trying to do: 这是我要执行的操作的一个示例:

if (bottomOfRange < variable < topOfRange) {
//Do stuff
}

I know that this: 我知道这:

if (bottomOfRange < varialbe && variable < topOfRange) {
//Do stuff
}

works, I'm just looking for an even more efficient way of comparing. 的作品,我只是在寻找一种更有效的比较方式。

Will this evaluate how I want, or will it act differently? 这会评估我的意愿,还是会有所不同?

No. This code will probably compile, depending on the types of the variables, but won't give the comparison you want. 不会。此代码可能会根据变量的类型进行编译,但不会提供所需的比较。 It will: 它会:

  • evaluate bottomOfRange < variable to give a boolean result; 计算bottomOfRange < variable以给出布尔结果;
  • promote that result to a numeric type with value 0 or 1; 将结果提升为数值类型0或1;
  • compare that value with topOfRange . 将该值与topOfRange进行比较。

If you want to compare a value against two other values, then you need to write out the two comparisons: 如果要将一个值与其他两个值进行比较,则需要写出两个比较:

if (bottomOfRange < variable && variable < topOfRange)

Assuming that bottomOfRange and topOfRange are built-in types, then no, it won't accomplish anything useful. 假设bottomOfRangetopOfRange是内置类型,则不会,它不会完成任何有用的工作。

If you want to badly enough, you could define a type that overloaded operator< to make it actually work though: 如果您想做的足够糟糕,可以定义一个重载operator<的类型,以使其实际上可以工作:

class range {
    int lower;
    int val;
    range(int lower, int val) : lower(lower), val(val) {}
public:
    bool operator < (int upper) {
        return lower < val && val < upper;
    }

    class start {
        int t;
    public:
        start(int t) : t(t) {}
        range operator < (int val) { return range(t, val); }
    };
};

#ifdef TEST

#include <iostream>

int main(){
    static char const *s [] = { "Out of range", "In range" };

    int variable = 20;
    range::start bottomOfRange(10);
    int topOfRange(30);

    if (bottomOfRange < variable < topOfRange)
        std::cout << "In range\n";
    else
        std::cout << "out of range\n";

    bottomOfRange = 30;
    topOfRange = 10;

    if (bottomOfRange < variable < topOfRange)
        std::cout << "In range\n";
    else
        std::cout << "Out of range\n";
}
#endif

Result: 结果:

In range
Out of range

Warning: this is intended primarily (purely?) as a curio. 警告:这主要(纯粹是?)用作古玩。 Although it obviously supports exactly what you've asked for, I'd be pretty hesitant about using it in my own code, and I'm not really recommending that you use it in yours either. 尽管它显然可以完全支持您的要求,但对于在我自己的代码中使用它,我还是很犹豫的,我也不建议您在自己的代码中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以在 c++20 中使用比较运算符在枚举值之间建立小于大于顺序的关系 - Is it possible to make a less-than greater-than ordering relationship between enum values using comparison operator in c++20 小于函数解引用指针 - Less-than function dereferencing pointers 如何在模板参数中使用 &gt;(大于)而不出现解析错误? - How to use > (greater-than) inside a template parameter and not get a parsing error? 在这段python代码中,大于号的含义是什么? - What does the greater-than symbol mean in this piece of python code? atomic_compare_exchange 用大于号代替等于号? - atomic_compare_exchange with greater-than instead of equals? 保留排序序列而没有小于谓词 - Keeping sorted sequence without a less-than predicate 为什么我的小于运算符没有处理? - Why isn't my less-than operator processing? 为什么 std::sort 在比较函数使用大于 (&gt;) 而不是大于或等于 (&gt;=) 时起作用? - Why does std::sort work when the comparison function uses greater-than (>), but not greater-than-or-equal (>=)? 几次执行后,小于运算符无法正常工作 - less-than operator doesn't work properly after few executions 不起作用:覆盖类的shared_ptr的默认小于号运算符 - Not working: override the default less-than operator of shared_ptr of a class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM