Home C&C++函数库 c++ 语法 程序源码 Linux C库

std::

unary_negate

类模板  <functional>

template <class Predicate> class unary_negate;

对一元函数对象求反的类
一元函数对象类,其调用返回与另一个一元函数相反的值,并传递给其构造函数。

unary_negate类型的对象通常使用函数not1构造。

它的行为定义如下:
C++98
template <class Predicate> class unary_negate
  : public unary_function <typename Predicate::argument_type,bool>
{
protected:
  Predicate fn_;
public:
  explicit unary_negate (const Predicate& pred) : fn_ (pred) {}
  bool operator() (const typename Predicate::argument_type& x) const {return !fn_(x);}
};

☲  模板参数


Predicate
一元函数对象类,定义了成员argument_type。

☲  成员类型


成员类型 定义 注释
argument_type T 成员operator()中参数的类型
result_type T 成员operator()返回的类型

☲  成员函数



constructor
构造一个对象,其函数调用返回与作为参数传递的对象相反的值。

operator()
返回与构造对象的函数对象相反的成员函数。


☣  示例



// unary_negate example
#include <iostream>     // std::cout
#include <functional>   // std::unary_negate
#include <algorithm>    // std::count_if

struct IsOdd_class {
  bool operator() (const int& x) const {return x%2==1;}
  typedef int argument_type;
} IsOdd_object;

int main () {
  std::unary_negate<IsOdd_class> IsEven_object (IsOdd_object);
  int values[] = {1,2,3,4,5};
  int cx;
  cx = std::count_if ( values, values+5, IsEven_object );
  std::cout << "There are " << cx << " elements with even values.\n";
  return 0;
}

输出:
There are 2 elements with even values.

🍄  另请参阅



not1 返回一元函数对象的结果相反的一元函数对象(类模板)
binary_negate 对二元函数对象求反的类(类模板)

联系我们 免责声明 关于CandCplus 网站地图