在条件语句中指定一个整数
assigning an integer in a conditional statement
我想将返回值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
}
您已经拥有了一些可读、可维护的代码。
- C++需要帮助从用户那里获得一个整数,并确保它在另外两个整数之间
- 当我们从/tp地址中添加/减去一个整数时会发生什么
- 如何将一个数组值合并为一个整数c++
- 为什么有时我输入一个整数,程序将第一个输入的数字打印成十进制数?
- 给定一个整数数组,需要在Max_Heap上运行操作。得到错误"segmentation fault",有什么想法吗?(C++)
- 我可以将迭代器递增一个整数吗?
- 有没有办法生成一个包含平方的序列,这些平方加起来就是一个整数平方?
- 为什么将两个浮点数相加会得到一个整数C++?
- 如果我们在其中输入一个整数,则字符会给出整数作为输出,但是当分配给它一个整数时,这不会发生。为什么?
- 给枚举一个整数,并在 C++ 中将其相关值作为字符串获取
- 按升序输出整数时,将最后一个整数放在新行上
- 编写一个程序,提示用户输入一个整数,然后输出数字的单个数字和数字的总和
- 给定一个整数 N>0,区间 [0, 2^N) 中有多少个整数正好有 N-1 个设置位?编写一个返回正确答案的简短函数
- 给定一个整数,将其表示为平方和
- c++interpret_cast是一个整数
- 以最少的步骤将给定整数转换为另一个整数
- 当 map 是一个整数数组并且由 operator[] 创建时,它是否初始化其映射类型
- 我需要将字符串(例如 A5)转换为一个字符串 (A) 和一个整数 (5)
- 如何交替从文件中获取一行数据和一个整数?
- 如何让这个C++代码从用户那里读取五个整数而不是一个整数?