gpt4 book ai didi

c++ - 了解 C++ 中循环的简写形式

转载 作者:行者123 更新时间:2023-11-30 01:58:46 25 4
gpt4 key购买 nike

我是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com