抛出'std :: out_of_range'的实例后调用terminate():basic_string :: erase

Dom*_*WTF 1 c++ stdstring erase outofrangeexception

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;
}
Run Code Online (Sandbox Code Playgroud)

我试图擦除传递给函数的字符串中的每个"afa""efe""ifi""ofo"和"ufu",但它给了我这个错误.我不知道我做错了什么..

Ker*_* SB 5

它应该是这样的:

while ((pos = buff.find("x")) != std::string::npos)
{
    // ...
}
Run Code Online (Sandbox Code Playgroud)

"未找到"通过返回npos而不是零来表示.零只是第一个角色.