다음을 통해 공유


unary_negate 클래스

지정된 단항 함수의 반환 값을 부정하는 멤버 함수를 제공하는 클래스 템플릿입니다. not_fn 위해 C++17에서는 사용되지 않습니다.

구문

template <class Predicate>
class unary_negate
    : public unaryFunction<typename Predicate::argument_type, bool>
{
    explicit unary_negate(const Predicate& Func);
    bool operator()(const typename Predicate::argument_type& left) const;
};

매개 변수

Func
부정할 단항 함수입니다.

left
부정할 단항 함수의 피연산자입니다.

Return Value

단항 함수의 부정

설명

클래스 템플릿은 단항 함수 개체 _Func 복사본을 저장합니다. 해당 멤버 함수 operator() 를 반환으로 정의합니다 !_Func(left).

unary_negate의 생성자는 직접 사용되는 경우가 거의 없습니다. 도우미 함수 not1을 사용하면 unary_negator 어댑터 조건자를 보다 쉽게 선언하고 사용할 수 있습니다.

예제

// 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;
}
The vector v1 = ( 0 5 10 15 20 25 30 35 )
The number of elements in v1 greater than 10 is: 5.
The number of elements in v1 not greater than 10 is: 3.