这句话到底是什么意思?
可能的重复:
这行代码是什么意思?
我正在阅读一篇关于Opencv,并遇到了这个:
for( i = 0; i < (faces ? faces->total : 0); i++ )
{
CvRect* r = (CvRect*)cvGetSeqElem(faces, i);
}
这行到底是什么意思:
<代码>我< (面孔?面孔->总数:0)
Possible Duplicate:
What does this line of code mean?
I was reading an article on Opencv, and came across this:
for( i = 0; i < (faces ? faces->total : 0); i++ )
{
CvRect* r = (CvRect*)cvGetSeqElem(faces, i);
}
What in the world is this line supposed to mean:
i < (faces ? faces->total : 0)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
它是条件运算符。
(faces ? faces->total : 0)
测试faces
。如果为 true,则返回faces->total
并与i
进行比较。否则,如果其计算结果为 false,则将i
与0
进行比较。It's the conditional operator.
The
(faces ? faces->total : 0)
testsfaces
. If it's true, thenfaces->total
is returned and compared withi
. Otherwise, if it evaluates to false,i
is compared to0
.编辑:根据 Femaref 的评论,我下面的代码(原始响应)不等效。
faces
可能通过调用cvGetSeqElem
进行修改,这反过来又意味着faces->total
也可能被修改。因此,条件可能在每次迭代中发生变化,如果faces
为 null (0
),则循环退出。尽管如此,它仍然令人困惑(明白我的意思!)。您可以轻松地检查
faces
是否为空并退出循环。循环计数器在每次迭代中可能会发生变化,这取决于 cvGetSeqElem 函数对其进行变异,而且只是 bleh。它是一个简写的 if 语句,称为条件运算符。它与以下内容非常相似:
因此,如果
faces
为 null,则count
为0
并且循环体永远不会执行,因为第一个测试失败。当然,您可以先检查它是否为空,然后一起避免整个事情。在我看来,该代码很丑陋。另外,如果
faces->total
是> 1 那么您只是在浪费周期,因为只有cvGetSeqElem
返回的最后一个值会保留。当然,该函数可能会产生其他所依赖的副作用,这将使代码比现在更加复杂。EDIT: Per Femaref's comment, my code below (original response) is not equivalent.
faces
is likely modified by callingcvGetSeqElem
, which in turn means thatfaces->total
is probably modified as well. So, the condition may change in each iteration and, iffaces
is null (0
), the loop exits.Still, it's confusing (got me!). You could just as easily check for
faces
being null and exit the loop. The loop counter potentially changes in each iteration, is dependent upon thecvGetSeqElem
function mutating it, and just bleh.It is a shorthand if statement called the conditional operator. It is much the same as:
So, if
faces
is null,count
is0
and the loop body never executes because the first test fails. Of course, you could just check first if that is null and avoid the whole thing together. That code is ugly IMO.Also, if
faces->total
is > 1 then you are just wasting cycles as only the last value returned bycvGetSeqElem
will persist. Of course, that function could have other side effects that are being relied upon, which would make the code even more convoluted than it already is.它迭代到作为结构中的成员的值。
如果结构指针为 NULL,则迭代零次。
这是一项安全功能,可防止取消引用 NULL 指针。
It's iterating up to a value that is a member in a structure.
If the structure pointer is NULL, then it iterates zero times.
It's a safety feature to prevent dereferencing a NULL pointer.
它相当于
(注意:这只是对 for 循环中部分的解释,而不是替换)
这是 C 中的一个技巧。faces 实际上是一个指向结构体的指针(可以通过以下方式看到 )
->
取消引用)。 C 中没有布尔类型,0 表示 false,其他值都表示 true。在这种情况下,指针隐式转换为 int 并在条件运算符。一旦指向结构的指针为
NULL
,循环就会终止。faces
在cvGetSeqElem
中更改。it is equivalent to
(Note: this is just an explanation for the part in the for loop, not a replacement)
This is a trick in C.
faces
is in fact a pointer to a struct (visible by the->
dereferencing). There is no Boolean type in C, 0 means false, every other value is true. In this case, the pointer is implicitly cast to int and than interpreted as Boolean in the conditional operator.The loop is terminated once the pointer to the struct is
NULL
.faces
is changed in thecvGetSeqElem
.三元运算符:“如果 faces 为 true,则 faces->total,否则为零”。
The ternary operator: "if faces is true, then faces->total, otherwise zero".