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

std::

binary_negate

类模板  <functional>

template <class Predicate> class binary_negate;

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

binary_negate类型的对象通常使用函数not2构造。

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

☲  模板参数


Predicate
一个二元函数对象类,定义了成员first_argument_type和second_argument_type。

☲  成员类型


成员类型 定义 注释
first_argument_type T 成员operator()中第一个参数的类型
second_argument_type T 成员operator()中第二个参数的类型
result_type T 成员operator()返回的类型

☲  成员函数



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

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


☣  示例



// binary_negate example
#include <iostream>     // std::cout
#include <functional>   // std::binary_negate, std::equal_to
#include <algorithm>    // std::mismatch
#include <utility>      // std::pair

int main () {
  std::equal_to<int> equality;
  std::binary_negate < std::equal_to<int> > nonequality (equality);
  int foo[] = {10,20,30,40,50};
  int bar[] = {0,15,30,45,60};
  std::pair<int*,int*> firstmatch,firstmismatch;
  firstmismatch = std::mismatch (foo,foo+5,bar,equality);
  firstmatch = std::mismatch (foo,foo+5,bar,nonequality);
  std::cout << "First mismatch in bar is " << *firstmismatch.second << "\n";
  std::cout << "First match in bar is " << *firstmatch.second << "\n";
  return 0;
}

输出:
First mismatch in bar is 0
First match in bar is 30

🍄  另请参阅



not2 返回二元函数对象的结果相反的二元函数对象(类模板)
unary_negate 对一元函数对象求反的类(类模板)
binary_function 二元函数对象基类 (类模板)

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