Partilhar via


Classe unary_negate

Uma classe do modelo que fornece uma função de membro que nega o valor de retorno de uma função unário especificada.

template<class Predicate> 
   class unary_negate 
      : public unary_function< 
         typename Predicate::argument_type, 
         bool>  
   { 
   public: 
   explicit unary_negate( 
      const Predicate& _Func 
   ); 
   bool operator()( 
      const typename Predicate::argument_type& _Left ) const; 
   };

Parâmetros

  • _Func
    A função unário a ser negada.

  • _Left
    O operando da função unário a ser negada.

Valor de retorno

A negação da função unário.

Comentários

A classe do modelo armazena uma cópia de um _Func unário do objeto de função*.* Define sua função de membro operator() como retornar o _Func de !(_Left).

O construtor de unary_negate é raramente usada diretamente. A função not1 auxiliar fornece um modo mais fácil de declarar e usar o predicado do adaptador de unary_negator .

Exemplo

// functional_unary_negate.cpp
// compile with: /EHsc
#include <vector>
#include <functional>
#include <algorithm>
#include <iostream>

using namespace std;

int main()
{
    vector<int> v1;
    vector<int>::iterator Iter;

    int i;
    for (i = 0; i <= 7; i++)
    {
        v1.push_back(5 * i);
    }

    cout << "The vector v1 = ( ";
    for (Iter = v1.begin(); Iter != v1.end(); Iter++)
        cout << *Iter << " ";
    cout << ")" << endl;

    vector<int>::iterator::difference_type result1;
    // Count the elements greater than 10
    result1 = count_if(v1.begin(), v1.end(), bind2nd(greater<int>(), 10));
    cout << "The number of elements in v1 greater than 10 is: "
         << result1 << "." << endl;

    vector<int>::iterator::difference_type result2;
    // Use the negator to count the elements less than or equal to 10
    result2 = count_if(v1.begin(), v1.end(),
        unary_negate<binder2nd <greater<int> > >(bind2nd(greater<int>(),10)));

    // The following helper function not1 also works for the above line
    // not1(bind2nd(greater<int>(), 10)));

    cout << "The number of elements in v1 not greater than 10 is: "
         << result2 << "." << endl;
}
  

Requisitos

Cabeçalho: <funcional>

Namespace: std

Consulte também

Referência

Segurança de threads na Biblioteca Padrão C++

Biblioteca de Modelos Padrão