I want to remove all items that do not meet the criteria. For example: delete all characters in a string that is not a number. My solution using boost :: is_digit worked well.
struct my_is_digit { bool operator()( char c ) const { return c >= '0' && c <= '9'; } }; int main() { string s( "1a2b3c4d" ); s.erase( remove_if( s.begin(), s.end(), !boost::is_digit() ), s.end() ); s.erase( remove_if( s.begin(), s.end(), !my_is_digit() ), s.end() ); cout << s << endl; return 0; }
Then I tried my own version, the compiler complained :( error C2675: unary '!': 'My_is_digit' does not define this operator or conversion to a type acceptable for a predefined operator
I could use the not1 () adapter, but I still think the operator! more significant in my current context. How could I realize that! like boost :: is_digit ()? Any idea?
Update
Follow the instructions of Charles Bailey, I received this piece of code, however, the output is nothing:
struct my_is_digit : std::unary_function<bool, char> { bool operator()( char c ) const { return isdigit( c ); } }; std::unary_negate<my_is_digit> operator !( const my_is_digit& rhs ) { return std::not1( rhs ); } int main() { string s( "1a2b3c4d" );
Any idea what is wrong?
Thanks,
Chan
source share