【问题标题】:terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::erase在抛出 'std::out_of_range' what(): basic_string::erase 的实例后调用终止
【发布时间】:2012-10-01 17:34:02
【问题描述】:
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”,但它给了我这个错误。 我不知道我做错了什么..

【问题讨论】:

    标签: c++ erase outofrangeexception stdstring


    【解决方案1】:

    应该是这样的:

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

    “未找到”通过返回npos 发出信号,而不是零。零只是第一个字符。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-04
      • 1970-01-01
      • 2021-08-25
      • 1970-01-01
      • 2022-11-14
      • 1970-01-01
      • 2019-02-08
      相关资源
      最近更新 更多