C++ 如何使用运算符对谓词函数求反!在C++;?

C++ 如何使用运算符对谓词函数求反!在C++;?,c++,stl,C++,Stl,我想删除所有不符合条件的元素。例如:删除字符串中所有非数字的字符。我使用boost::的解决方案运行良好 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

我想删除所有不符合条件的元素。例如:删除字符串中所有非数字的字符。我使用boost::的解决方案运行良好

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;
}
struct my_是数字{
布尔运算符()(字符c)常量{

返回c>='0'&&c您应该能够使用
std::not1

std::unary_negate<my_is_digit> operator!( const my_is_digit& x )
{
    return std::not1( x );
}
我如何实现这样一个!比如boost::is_digit()

…大概您可以查看构成
is_digit
实现的代码?您将看到和相关代码:

template<typename PredT>
inline detail::pred_notF<PredT>
operator!( const predicate_facade<PredT>& Pred )
{
// Doing the static_cast with the pointer instead of the reference
// is a workaround for some compilers which have problems with
// static_cast's of template references, i.e. CW8. /grafik/
return detail::pred_notF<PredT>(*static_cast<const PredT*>(&Pred)); 
}
模板
内联详细信息::pred_notF
运算符!(const谓词\u facade和Pred)
{
//使用指针而不是引用执行静态_转换
//是一些编译器的解决方法,这些编译器在
//模板引用的静态类型转换,即CW8./grafik/
返回详细信息::pred_notF(*static_cast(&pred));
}

我很确定您不需要为
std::not1
明确提供类型。它是一个模板函数,可以从对象推断类型,不是吗?此外,谓词需要从
一元函数派生出来。
@Charles Bailey:谢谢您的快速回复。虽然我可以使用not1,但我仍然更喜欢boost::is\u dig的方式Charles Bailey:我很确定<代码> STD::NOT1 < /CUT>是函数模板。C++标准20.3.5表示<代码> STD::NOT1< /COD>有签名>模板UNYALYONETNOTENET1(const谓词和PRED)
,这是一个函数声明。@Chan:好吧,你可以在你的函子中添加一个重载的
操作符来返回一个被求反的函子,但这只是将求反的函子移到其他地方。@Chan:只需添加一个自由函数:
std::一元否定操作符!(const my_is_digit&x){return std::not1(x)}
谢谢你的建议,我实际上检查了boost::is_digit的源代码。但是,它比我想象的要复杂得多。所以我正在寻找一个更简单的解决方案。
std::一元函数
是错误的。如果你指的是我,我的名字是“Charles Bailey”,而不是“Bailey”@Charles Bailey:我真的很抱歉打错了你的全名。我已经编辑过了。我能问你std::一元函数有什么问题吗?^^!谢谢。好吧,应该是
std::一元函数
。如果你不想少打一点,请随意称呼be Charles。缩写我的名字在我的语言/文化/个人看来是行不通的。@Charles贝利:刚刚看到你的答案。明白了;)!非常感谢你的耐心。^查尔斯·贝利:谢谢你的指点。为了跨文化,从现在起我会拼出全名。谢谢。
#include <string>
#include <algorithm>
#include <functional>
#include <iostream>
#include <ostream>

struct my_is_digit : std::unary_function<char, bool>
{
    bool operator()(char c) const
    {
        return c >= '0' && c <= '9';
    }
};

std::unary_negate<my_is_digit> operator!( const my_is_digit& x )
{
    return std::not1( x );
}

int main() {
    std::string s( "1a2b3c4d" );
    s.erase( std::remove_if( s.begin(), s.end(), !my_is_digit() ), s.end() );
    std::cout << s << std::endl;
    return 0;
}
template<typename PredT>
inline detail::pred_notF<PredT>
operator!( const predicate_facade<PredT>& Pred )
{
// Doing the static_cast with the pointer instead of the reference
// is a workaround for some compilers which have problems with
// static_cast's of template references, i.e. CW8. /grafik/
return detail::pred_notF<PredT>(*static_cast<const PredT*>(&Pred)); 
}