简体   繁体   English

为什么擦除命令结束我的while循环?

[英]Why does the erase command end my while loop?

So I have a piece of code which works pretty well. 所以我有一段很好的代码。 The goal was to filter through my whatsapp - chat history. 目标是过滤我的whatsapp - 聊天记录。 First thing I wanted was to sort out such that only my messages which start with 'Ben: ...' are sorted out and the rest discarded. 我想要的第一件事就是整理出来,只有以'Ben:...'开头的消息才会被整理掉,其余的被丢弃。

I succeeded with this but only because of a weird feature I can't understand. 我成功了,但这只是因为我无法理解的一个奇怪的功能。 Because of some older attempts I have an erase command in my code, that for now has no real purpose. 由于一些较旧的尝试, 我在我的代码中有一个擦除命令,现在没有真正的目的。 Yet without it my while loop, keeps looping infinitely. 然而没有它我的while循环,保持无限循环。

Why does the erase command help me finish the loop ? 为什么擦除命令可以帮助我完成循环?

I tried removing it, and played around with other ideas. 我尝试删除它,并玩弄其他想法。 Nothing worked. 没有任何效果。

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main(){
    string text;
    string deli = "Ben:";

    string token;
    size_t pos = 0;

    ifstream myfile;
    myfile.open("WAEZ.txt");

    while( getline(myfile, text)){
        while((pos = text.find(deli)) != std::string::npos){
            token = text.substr(pos,std::string::npos);
            cout << token << endl;
            text.erase(0, pos + deli.length());
        }
    }    
}

I'd like to have this code work without the erase command which I am not using. 我想让这段代码在没有我不使用的擦除命令的情况下工作。

text.find(deli)) is finding something in your string, and if it is found, then text.find(deli)) != std::string::npos is true. text.find(deli))在你的字符串中找到一些东西,如果找到了,那么text.find(deli)) != std::string::npos是真的。 If you call it again with the same text and the same deli , then the result will be the same. 如果您使用相同的text和相同的deli再次调用它,那么结果将是相同的。

In your loop, text.erase(0, pos + deli.length()); 在你的循环中, text.erase(0, pos + deli.length()); changes the string text . 更改字符串text Without this part, nothing in your loop changes the text or the deli . 没有这个部分,循环中的任何内容都不会改变textdeli That's why it results in an infinite loop: if text.find(deli)) != std::string::npos is true the first time, it is true forever and nothing will stop it as there also isn't any break or any other kind of logic in your code that can get out of the loop. 这就是为什么它会导致一个无限循环:如果text.find(deli)) != std::string::npostrue第一次,这是事实永远,什么也阻止不了它也没有任何break或代码中可以退出循环的任何其他类型的逻辑。

To mimic your code without erase, you might use the extra parameter of find to specify start position of the search: 要模拟代码而不擦除,可以使用find的额外参数来指定搜索的起始位置:

while (getline(myfile, text)) {
    std::size_t origin = 0;
    while((pos = text.find(deli, origin)) != std::string::npos){
        token = text.substr(pos, std::string::npos);
        cout << token << endl;
        origin += deli.length();
    }
}

But as you describe your problem, C++20 starts_with seems fulfill your needs: 但是当你描述你的问题时,C ++ 20 starts_with似乎满足了你的需求:

while (getline(myfile, text)) {
    if (text.starts_with(deli)){
        std::cout << text << std::endl;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM