complexity is O(n^2). Use str.erase(remove_if(...), str.end());