As was denoted in the comments, you are missing the include for std::isspace
, namely <cctype>
. But even then you won't have success, because isspace is overloaded see here.
The solution to the overload problem would be to explicitly cast the function pointer to the desired function sgnature:
str.erase(remove_if(str.begin(), str.end(), static_cast<int(*)(int)>(&std::isspace)),str.end());
However, as has been noted in the comments, the isspace
used here has undefined behavior if it gets non-ASCII characters. In that case it would be preferable to use the templated version taking a locale as second parameter:
C++14:
str.erase(
remove_if(str.begin(), str.end(),
[](auto c){ return isspace(c, cin.getloc());}
),
str.end());
C++11: as above, with the lambda taking a char c
as parameter (no polymorphic lambdas in C++11).
C++03 with boost: using boost::bind
to create the predicate for remove_if
str.erase(
remove_if(str.begin(), str.end(),
boost::bind(&std::isspace<char>, _1, boost::ref(cin.getloc()))
),
str.end());
C++03 without boost: defining a handwritten functor as predicate
struct IsSpace {
bool operator()(char c) {
return std::isspace(c, cin.getloc());
}
};
str.erase(
remove_if(str.begin(), str.end(),
IsSpace()
),
str.end());