我是 C++ 的新手。我试图从我的书中找出这个循环的简写示例,它又长又丑,我想知道是否可以用更简洁的方式重新编写它。忽略其中的函数,例如 before() 和其他函数,它们是我正在处理的链表程序的一部分。我只是从我的书中不明白如何以更“传统”的方式重新编写循环。提前致谢!
fooExample(string str){
string s = toLower(str);
for(books->setEnd();!books->atStart() && (toLower(books->getInfo().getAuthor()).search(s)==string::npos);books->before());
}
最佳答案
C++ 中 for 循环的形式如下所示:
for(initialisation; condition; incrementation)
{
//code
}
所以你可以做类似的事情
for(unsigned int i = 0; i < 10; ++i)
{
std::cout << "i = " << i << std::endl;
}
你代码中的原理是一样的;有一个初始化,一个条件,而不是真正的“增量”,而是循环的每次迭代都会发生的事情(我想它会出现在上一本书中)。
关于c++ - 了解 C++ 中循环的简写形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16865964/