2013-10-22

std::string trim space error, compile error!



I just want to remove all the "space" in the strings like "Hello World!  ".
Code Like this:
std::string str("Hello World! ");
str.erase(remove_if(str.begin(), str.end(), isspace), str.end());

But I got the following compile error:
No matching function for call to 'remove_if(__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, <unresolved overloaded function type>)'

Finally, I got the solution , it should add one "::" before isspace.
    str.erase(remove_if(str.begin(), str.end(), ::isspace), str.end());
isspace are overloaded functions, so the compiler doesn't know which implementation should be used

No comments:

Post a Comment