简体   繁体   中英

terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::erase

string Farfallino::decode(string buff) {

string stringa;
size_t pos;

while(1) {
    while(pos = (buff.find("afa"))) {
        buff.erase(pos, 3);
        buff.insert(pos, "a");
    }
    while(pos = (buff.find("efe"))) {
        buff.erase(pos, 3);
        buff.insert(pos, "e");
    }
    while(pos = (buff.find("ifi"))) {
        buff.erase(pos, 3);
        buff.insert(pos, "i");
    }
    while(pos = (buff.find("ofo"))) {
        buff.erase(pos, 3);
        buff.insert(pos, "o");
    }
    while(pos = (buff.find("ufu"))) {
        buff.erase(pos, 3);
        buff.insert(pos, "u");
    }
}

return stringa;
}

I'm trying to erase every "afa" "efe" "ifi" "ofo" and "ufu" that are in the string passed to the function, but it gives me this error. I have no idea what i'm doing wrong..

It should be something like this:

while ((pos = buff.find("x")) != std::string::npos)
{
    // ...
}

"Not found" is signaled by returning npos , not zero. Zero would just be the first character.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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