简体   繁体   English

比较几个整数值的相等性

[英]Comparison of several integer values for equality

If I have two integer values I can test for equality simply: 如果我有两个整数值,我可以简单地测试相等性:

if (a == b)

if I have three integer values then I could do something like: 如果我有三个整数值,那么我可以这样做:

if ((a == b) && (a == c) && (b == c))

I've got a situation where I've got 6 values to test for equality, this approach is going to get verbose 我有一个情况,我有6个值来测试相等,这种方法将变得冗长

Is their a better, and more C idiomatic (c-onic?) means to achieve this? 他们是一个更好的,更多C惯用(c-onic?)意味着实现这一目标?

actually, just having written this I can see that: 实际上,只要写完这些就可以看到:

if ((a == b) && (b == c) && (c == d)... )

is logically ok, ie you don't need all the combinations as a == b and b == c implies a == c 在逻辑上是正确的,即你不需要所有的组合作为a == bb == c暗示a == c

But in terms of clarity of expression how close can I get to: 但就表达的清晰度而言,我有多接近:

if (a == b == c) ? if (a == b == c)

I can show you how to do it in a variadic macro, so we can write this: 我可以告诉你如何在一个可变参数宏中做到这一点,所以我们可以这样写:

if(EQI(a, b, c, d, e, f))

Here's the code: 这是代码:

#include <stdarg.h>
#include <stddef.h>

#define NUMARGS(...) (sizeof((int[]){__VA_ARGS__}) / sizeof(int))
#define EQI(...) ( \
    alleqi(NUMARGS(__VA_ARGS__), __VA_ARGS__))

/* takes count number of int arguments */
int alleqi(size_t count, ...)
{
    va_list va;
    va_start(va, count);

    int v0 = va_arg(va, int);

    for (; count > 1; count--) {
        if (v0 != va_arg(va, int)) {
            break;
        }
    }

    va_end(va);
    return count == 1;
}

Please be advised the above only works for int arguments (and probably other types whose size is the same as int , though I am bracing for someone to accuse me of promoting undefined behavior if I advertise that!). 请注意,以上内容仅适用于int参数(可能还有其他类型与int相同的类型,但是如果我做广告的话,我会支持某人指责我宣传未定义的行为!)。

Thanks to this prior post for figuring out how to count arguments in a variadic macro: https://stackoverflow.com/a/2124433/4323 感谢这篇先前的帖子,想弄清楚如何计算可变参数宏中的参数: https//stackoverflow.com/a/2124433/4323

You're not going to get very close to a == b == c . 你不会非常接近a == b == c

To cause experienced C programmers the least surprise, I think this is best is 为了给经验丰富的C程序员带来最少的惊喜,我认为这是最好的

if(a == b && b == c && c == d && d == e && e == f)

ie basically like what you have already, with less parentheses. 即基本上就像你已经拥有的那样,括号较少。 I'm not always opposed to adding parentheses that make no difference to the compiler to help humans who don't know all the precedence rules, but I think comparisons separated by && (with no || mixed in) are a simple enough case that the parentheses are more clutter than they're worth. 我并不总是反对添加对编译器没有任何影响的括号,以帮助那些不了解所有优先规则的人,但我认为用&&分隔的比较(没有||混合)是一个简单的案例,圆括号比它们的价值更混乱。

If the variable names are long, putting it all on one line isn't going to be pretty, so maybe 如果变量名称很长,那么把它全部放在一行就不会很漂亮,所以也许吧

if(a == b &&
   b == c &&
   c == d &&
   d == e &&
   e == f)

or this might be better since the duplication of the the a in every line will be immediately noticeable: 或者这可能会更好,因为每行中a的重复将立即引人注目:

if(a == b &&
   a == c &&
   a == d &&
   a == e &&
   a == f)

If you really want it to be compact, the p99 preprocessor library offers this: 如果你真的想要它是紧凑的, p99预处理器库提供了这个:

#include "p99_for.h"

if(P99_ARE_EQ(a,b,c,d,e,f))

which looks like John Zwinck's answer but doesn't require a helper function. 看起来像John Zwinck的答案,但不需要辅助功能。

If you want to check six values, one approach would be to store them in an array 如果要检查六个值,一种方法是将它们存储在一个数组中

int a[6];

equals = 1;
for (int i=0; i<5; i++)
{
    if (a[i] != a[i+1])
    {
       equals = 0;
       break;
    }
}

Approaches using arrays require you to change your data structure into an array. 使用数组的方法要求您将数据结构更改为数组。

Approaches using a variadic macro requires an additional function and function call (overhead). 使用可变参数宏的方法需要额外的函数和函数调用(开销)。

Except for these two approaches, the only other way I see is to write them out. 除了这两种方法之外,我看到的另一种方法是将它们写出来。 I feel the best way, or the more clear way, is to take the first variable and compare it to all other variables. 我觉得最好的方法,或者更明确的方法是采用第一个变量并将其与所有其他变量进行比较。 By implication then either all variabes are equal, or at least two variables are unequal: 通过暗示,要么所有变量都相等,要么至少两个变量不相等:

if ((a == b) && (a == c) && (a == d)
&&  (a == e) && (a == f) && (a == g) ...)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM