문제

The code below wants to take a string and output only lowercase letters from the English alphabet.

string simplifyString(string word)
{
    word.erase(remove_if(word.begin(), word.end(), [](char letter){return !isalpha(letter);}));
    transform(word.begin(), word.end(), word.begin(), tolower);
    return word;
}

int main()
{
    string s = "a.b.c.d.e.f.g.h.";
    cout << simplifyString(s) << endl;;
    return 0;
}

The output is: abcdefgh.f.g.h.

So the code works and then stops working. What the heck is going on?

도움이 되었습니까?

해결책

word.erase(remove_if(...));

That's close but not quite right. That only removes the first element set aside by remove_if. You want to remove the entire range:

word.erase(remove_if(...), word.end());
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top