Complexity is O(n^2). Use str.erase(std::remove_if(...), str.end());