C ++:在while循环中更新函数内部的变量值

C++: update variable value inside function in a while loop

我不是 C++ 的专家,我正在编写一个程序来读取 html 文件的一行中的多个 URL,所以我写了这段代码:

ifstream bf;
short chapters=0;
string blkhtml;
string blktmpfile; //given
string urldown;    //given
size_t found = 0, limit;

    while(getline(bf, blkhtml)){
            while((blkhtml.find(urldown, found) != string::npos) == 1){
                found = blkhtml.find(urldown);
                limit = blkhtml.find("\"", found);
                found=limit + 1;
                chapters++;
            }
    }

我的问题是 found 没有更新以用于 while 条件。正如我所见,除非另一个 std::string class(对于字符串,str.erase() 更新它的值,否则 std::string classes 不会更新,但是(str.at() = '') doesn't),如果我想在每次循环开始时更新 "found",并且针对条件,我可以在这里做什么。

我想做的是:

我找遍了 cplusplus.com 和 cppreference.com,但没有找到对我有帮助的东西。

我想过std::list::remove在一个从0到9的每个数字循环,然后给它一个新的值,但我不知道这是不是最好的选择。

问题是你每次都从头开始搜索:

while((blkhtml.find(urldown, found) != string::npos) == 1){
    found = blkhtml.find(urldown); // Searches from beginning of the string

这应该是:

while((blkhtml.find(urldown, found) != string::npos) == 1){
    found = blkhtml.find(urldown, found); // Searches from "found"

或者只搜索一次,可以放在while子句中:

while((found = blkhtml.find(urldown, found)) != string::npos){

此外,您不会在每次读取新行时都重置 found

while(getline(bf, blkhtml)){
    found = 0;