我遇到了一个for循环,条件是参数(p)本身。循环什么时候停止?我在我的 C++ 书籍中没有看到这种情况。
for (PDFS *p = e->prev ; p ; p = p->prev) {
push_back (p->edge);
edge[p->edge->id] = vertex[p->edge->from] = vertex[p->edge->to] = 1;
}
最佳答案
循环会在 p
时停止是NULL
.在循环中,您不需要显式检查条件 p !=NULL
,或者在 C++11 中,p != nullptr
.
类似的代码也写在你有空终止的 c 字符串中:
char str[] = "its null-terminated string";
for(size_t i = 0 ; str[i] ; ++i)
std::cout << str[i] << std::endl;
在这种情况下,您不需要检查条件 i < strlen(str)
或类似的东西。
关于c++ - 当 for 循环的停止条件是一个单独的指针变量时,这意味着什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9704564/