gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:25:13 24 4
gpt4 key购买 nike

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 发出信号,而不是零。零只是第一个字符。

关于c++ - 在抛出 'std::out_of_range' what(): basic_string::erase 实例后终止调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12678137/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com