在条件语句中指定一个整数

assigning an integer in a conditional statement

本文关键字:一个 整数 条件 语句      更新时间:2023-10-16

我想将返回值CountBitDifference分配给currentDistance。相反,它会返回0或1,这取决于条件语句是false还是true。有没有办法在条件语句中分配currentDistance值,否则我将不得不在其他地方执行。

if (int currentDistance = CountBitDifference(m0.ptr<unsigned char>(matches[i].queryIdx),
                                    m1.ptr<unsigned char>(matches[i].trainIdx),
                                    cascadeSize, cascadeByteIndex) >= 8) 

您的代码将currentDistance初始化为>=的结果。如果要将CountBitDifference返回的值分配给currentDistance将该值与8进行比较——所有这些都在同一表达式中——则需要括号。

int currentDistance;
if ( ( currentDistance = CountBitDifference(m0.ptr<unsigned char>(matches[i].queryIdx),
                                    m1.ptr<unsigned char>(matches[i].trainIdx),
                                    cascadeSize, cascadeByteIndex)) >= 8) 

或者,更清楚地说:

int currentDistance = CountBitDifference(m0.ptr<unsigned char>(matches[i].queryIdx),
                                    m1.ptr<unsigned char>(matches[i].trainIdx),
                                    cascadeSize, cascadeByteIndex);
if(currentDistance >= 8)
if (int currentDistance = CountBitDifference(m0.ptr<unsigned char>(matches[i].queryIdx),
                                    m1.ptr<unsigned char>(matches[i].trainIdx),
                                    cascadeSize, cascadeByteIndex) >= 8) 

只是太难阅读了。请尝试以下代码。

 int currentDistance = CountBitDifference(m0.ptr<unsigned char>(matches[i].queryIdx) 
                                           ,m1.ptr<unsigned char>(matches[i].trainIdx)
                                           ,cascadeSize, cascadeByteIndex);
   if(currentDistance >= 8) { }

还要记住,当有人在阅读你的代码时,可能是你,他们/你可能不记得=>=运算符的关联性以及其中一个运算符的优先级。因此,最好将这两个语句分成多行。可读性很重要,不必要的简洁就是愚蠢。

应该没有任何理由要这样做。。。

在if语句之前进行初始化同样容易,而且可读性更强。如果你不需要保持CountBitDifference的值,为什么你不能这样做:

if (CountBitDifference(m0.ptr<unsigned char>(matches[i].queryIdx),
                                m1.ptr<unsigned char>(matches[i].trainIdx),
                                cascadeSize, cascadeByteIndex) >= 8)

如果你需要保留它,就这样做:

int currentDistance = CountBitDifference(m0.ptr<unsigned char>(matches[i].queryIdx),
                                m1.ptr<unsigned char>(matches[i].trainIdx),
                                cascadeSize, cascadeByteIndex);
if (currentDistance >= 8)

首先,我会转储日志以确认CountBitDifference表达式正在执行它应该执行的操作。如果是,这似乎是一个奇怪的作用域错误。在语句中分配currentDistance之前,定义并初始化它会让你丧命吗?

另外,if语句检查此函数是否成功。。。你为什么这么做?

编辑:我完全错过了表达式末尾的比较。这有点笨拙,我会认真考虑把它分解成一个返回布尔值的函数。8也是一个神奇的数字。

编辑:试试这个,返回一个整数,并作为一个函数鼓励重复使用。

public int getBitDifference() {
    //assumes you have declared your variables in a greater scope
    //otherwise you will have to pass your variables to the function... hard to say
    //without seeing all the code
    return CountBitDifference(m0.ptr<unsigned char>(matches[i].queryIdx),
                                m1.ptr<unsigned char>(matches[i].trainIdx),
                                cascadeSize, cascadeByteIndex)

}

然后:

if (getBitDifference() >= 8) {
//do stuff here
}

可读性更强。同时声明8为常数BIT_DISTANCE_MARGIN以避免使用幻数,因此

if (getBitDifference() >= BIT_DISTANCE_MARGIN) {
//do stuff here
}

您已经拥有了一些可读、可维护的代码。

相关文章: