如何使用运算符来否定谓词函数!在 C++ 中?
我想删除所有不满足标准的元素。例如:删除字符串中所有非数字字符。我使用 boost::is_digit 的解决方案效果很好。
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;
}
然后我尝试了我自己的版本,编译器抱怨:( 错误 C2675:一元 '!' :“my_is_digit”没有定义此运算符或转换为预定义运算符可接受的类型
我可以使用 not1() 适配器,但我仍然认为运算符!在我目前的背景下更有意义。我怎么能实现这样的!像 boost::is_digit() 一样?有什么想法吗?
更新
按照 Charles Bailey 的指示,我编译了此代码片段,但输出什么也没有:
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" );
//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;
}
知道出了什么问题吗?
谢谢,
陈
I want to erase all the elements that do not satisfy a criterion. For example: delete all the characters in a string that are not digit. 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 a conversion to a type acceptable to the predefined operator
I could use not1() adapter, however I still think the operator ! is more meaningful in my current context. How could I implement such a ! like boost::is_digit() ? Any idea?
Update
Follow Charles Bailey's instruction, I got this code snippet compiled, 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" );
//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;
}
Any idea what was wrong?
Thanks,
Chan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该能够使用
std::not1
。为此,您必须
#include
并从实用程序类std::unary_function
派生您的
。这纯粹是一个 typedef 帮助器,不会给函子增加运行时开销。my_is_digit
仿函数。字符,布尔>完整的工作示例:
You should be able to use
std::not1
.For this to work you have to
#include <functional>
and derive yourmy_is_digit
functor from the utility classstd::unary_function< char, bool >
. This is purely a typedef helper and adds no runtime overhead to your functor.Complete working example:
...大概你可以看看形成
is_digit
实现的代码?您将看到predicate_facade
以及相关代码:...presumably you could look at the code that forms the
is_digit
implementation? You will seepredicate_facade
and the relevant code: