類模板
<functional>

std::binary_negate

template <class Predicate> class binary_negate;
二元函式物件類,其呼叫返回建構函式傳遞的另一個二元函式的相反值。
此類是一個二元函式物件類,並定義了成員 first_argument_typesecond_argument_typeresult_type

型別為 binary_negate 的物件通常使用函式 not2 來構造。

此類定義了與以下行為相同的行為:

1
2
3
4
5
6
7
8
9
10
11
12
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); }
};
1
2
3
4
5
6
7
8
9
10
11
template <class Predicate> class binary_negate
{
protected:
  Predicate fn_;
public:
  explicit binary_negate (const Predicate& pred) : fn_ (pred) {}
  bool operator() (const typename Predicate::argument_type& x) const {return !fn_(x,y);}
  typedef typename Predicate::first_argument_type  first_argument_type;
  typedef typename Predicate::second_argument_type second_argument_type;
  typedef bool result_type;
};

模板引數

謂詞
一個二元函式物件類,定義了成員 first_argument_typesecond_argument_type

成員型別

成員型別定義說明
first_argument_typeT成員 operator() 的第一個引數的型別
second_argument_typeT成員 operator() 的第二個引數的型別
result_typeT成員 operator() 返回的型別

成員函式

建構函式
構造一個物件,該物件的函式呼叫返回與其構造時傳入的物件相反的值。
operator()
成員函式,返回與構造該物件時使用的函式物件相反的值。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 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


另見