繁体   English   中英

抛出'std :: out_of_range'what():basic_string :: erase实例后终止调用

[英]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;
}

我正在尝试删除传递给函数的字符串中的每个“ afa”,“ efe”,“ ifi”,“ ofo”和“ ufu”,但它给了我这个错误。 我不知道我在做什么错..

应该是这样的:

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

通过返回npos而不是零来表示“未找到”。 零将只是第一个字符。

暂无
暂无

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

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