Lov*_*eTW 1 c++ syntax for-loop
我遇到了一个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;
}
Run Code Online (Sandbox Code Playgroud)
当循环将停止p为NULL.在循环中,您不需要显式检查条件p !=NULL,或者在C++ 11中p != nullptr.
类似的代码也写成了你的空终止c-string:
char str[] = "its null-terminated string";
for(size_t i = 0 ; str[i] ; ++i)
std::cout << str[i] << std::endl;
Run Code Online (Sandbox Code Playgroud)
在这种情况下,您无需检查条件i < strlen(str) 或类似情况.
| 归档时间: |
|
| 查看次数: |
172 次 |
| 最近记录: |