1
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」を消去しようとしていますが、このエラーが発生します。私は何が間違っているのか分かりません..

4

1 に答える 1

5

次のようになります。

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

「見つかりません」はnpos、ゼロではなく を返すことで通知されます。ゼロは最初の文字にすぎません。

于 2012-10-01T17:35:21.613 に答える