remove algorithm
hey guys i cant seem to understand why de following code is not working::it gives me an error no matching function for call.....
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
string name="this is not nyc***";
name.erase(remove(name.begin(), name.end(),isspace), name.end());
cout<<name.size()<<endl;
}
|
If you want to pass a predicate function you have to use std::remove_if instead of std::remove.
http://www.cplusplus.com/reference/algorithm/remove_if/
Another problem is that there are multiple versions of isspace
http://www.cplusplus.com/reference/cctype/isspace/
http://www.cplusplus.com/reference/locale/isspace/
and the compiler doesn't know which one to pick.
To force the correct version you could use a cast
|
name.erase(remove_if(name.begin(), name.end(), static_cast<int(*)(int)>(isspace)), name.end());
|
Or you could specify the template argument types explicitly
|
name.erase(remove_if<string::iterator, int(*)(int)>(name.begin(), name.end(), isspace), name.end());
|
Or you could create another function, or use a lambda function, that calls the isspace function internally.
|
name.erase(remove_if(name.begin(), name.end(), [](char x){return isspace(x);}), name.end());
|
wow dats great,, only thot isalpha was of type cctype....it works thanx again
Topic archived. No new replies allowed.