다음을 통해 공유


unary_negate 클래스

지정된 단항 함수의 반환 값을 무효화하는 멤버 함수를 제공하는 템플릿 클래스.

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; 
   };

매개 변수

  • _Func
    The unary function to be negated.

  • _Left
    The operand of the unary function to be negated.

반환 값

The negation of the unary function.

설명

The template class stores a copy of a unary function object _Func. It defines its member function operator() as returning !_Func(_Left).

The constructor of unary_negate is rarely used directly. The helper function not1 provides an easier way to declare and use the unary_negator adaptor predicate.

예제

// 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;
}
  

요구 사항

헤더: <기능>

네임스페이스: std

참고 항목

참조

C++ 표준 라이브러리의 스레드 보안

표준 템플릿 라이브러리