这句话到底是什么意思?

发布于 2024-12-10 04:02:24 字数 479 浏览 0 评论 0原文

可能的重复:
这行代码是什么意思?

我正在阅读一篇关于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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夜声 2024-12-17 04:02:24

它是条件运算符

(faces ? faces->total : 0) 测试 faces。如果为 true,则返回 faces->total 并与 i 进行比较。否则,如果其计算结果为 false,则将 i0 进行比较。

It's the conditional operator.

The (faces ? faces->total : 0) tests faces. If it's true, then faces->total is returned and compared with i. Otherwise, if it evaluates to false, i is compared to 0.

ら栖息 2024-12-17 04:02:24

编辑:根据 Femaref 的评论,我下面的代码(原始响应)等效。 faces 可能通过调用 cvGetSeqElem 进行修改,这反过来又意味着 faces->total 也可能被修改。因此,条件可能在每次迭代中发生变化,如果 faces 为 null (0),则循环退出。

尽管如此,它仍然令人困惑(明白我的意思!)。您可以轻松地检查faces 是否为空并退出循环。循环计数器在每次迭代中可能会发生变化,这取决于 cvGetSeqElem 函数对其进行变异,而且只是 bleh。


它是一个简写的 if 语句,称为条件运算符。它与以下内容非常相似:

int count = 0;
if( faces )
  count = faces->total;

for( i = 0; i < count; i++ ) { 
    CvRect* r = (CvRect*)cvGetSeqElem(faces, i);
}

因此,如果 faces 为 null,则 count0 并且循环体永远不会执行,因为第一个测试失败。当然,您可以先检查它是否为空,然后一起避免整个事情。在我看来,该代码很丑陋。

if( faces ) {
    // do loop
}

另外,如果faces->total是> 1 那么您只是在浪费周期,因为只有 cvGetSeqElem 返回的最后一个值会保留。当然,该函数可能会产生其他所依赖的副作用,这将使代码比现在更加复杂。

EDIT: Per Femaref's comment, my code below (original response) is not equivalent. faces is likely modified by calling cvGetSeqElem, which in turn means that faces->total is probably modified as well. So, the condition may change in each iteration and, if faces 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 the cvGetSeqElem function mutating it, and just bleh.


It is a shorthand if statement called the conditional operator. It is much the same as:

int count = 0;
if( faces )
  count = faces->total;

for( i = 0; i < count; i++ ) { 
    CvRect* r = (CvRect*)cvGetSeqElem(faces, i);
}

So, if faces is null, count is 0 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.

if( faces ) {
    // do loop
}

Also, if faces->total is > 1 then you are just wasting cycles as only the last value returned by cvGetSeqElem 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.

泪冰清 2024-12-17 04:02:24

它迭代到作为结构中的成员的值。

如果结构指针为 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.

丶视觉 2024-12-17 04:02:24

它相当于

int output;

if(faces == 0)
  output = 0;
else
  output = faces->total;

(注意:这只是对 for 循环中部分的解释,而不是替换)

这是 C 中的一个技巧。faces 实际上是一个指向结构体的指针(可以通过以下方式看到 ) -> 取消引用)。 C 中没有布尔类型,0 表示 false,其他值都表示 true。在这种情况下,指针隐式转换为 int 并在条件运算符。

一旦指向结构的指针为NULL,循环就会终止。 facescvGetSeqElem 中更改。

it is equivalent to

int output;

if(faces == 0)
  output = 0;
else
  output = faces->total;

(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 the cvGetSeqElem.

舂唻埖巳落 2024-12-17 04:02:24

三元运算符:“如果 faces 为 true,则 faces->total,否则为零”。

The ternary operator: "if faces is true, then faces->total, otherwise zero".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文