functional_not2

functional_not2 (std::not2: 이진 조건자의 부정을 만드는 헬퍼 함수)

std::not2는 C++ 표준 라이브러리에서 제공하는 헬퍼 함수예요. 이 함수는 전달받은 이진 조건자(binary predicate)의 부정을 반환하는 함수 객체를 만들어 줘요. C++17에서 deprecated로 지정되었고 C++20에서 제거되었어요.

출처: cppreference

본문

<functional> 헤더에 정의되어 있어요.

template < class Predicate > std::binary_negate<Predicate> not2(const Predicate& pred); (C++14까지)
template < class Predicate > constexpr std::binary_negate<Predicate> not2(const Predicate& pred); (C++14부터) (C++17에서 deprecated) (C++20에서 제거)

설명

std::not2는 전달된 이진 조건자 함수의 부정(complement)을 반환하는 함수 객체를 생성하는 헬퍼 함수예요. 생성되는 함수 객체의 타입은 std::binary_negate<Predicate>예요.

이진 조건자 타입은 두 멤버 타입인 first_argument_typesecond_argument_type을 정의해야 하며, 이 타입들은 조건자의 매개변수 타입으로 변환 가능해야 해요. std::owner_less, std::ref, std::cref, std::plus, std::minus, std::multiplies, std::divides, std::modulus, std::equal_to, std::not_equal_to, std::greater, std::less, std::greater_equal, std::less_equal, std::logical_not, std::logical_or, std::bit_and, std::bit_or, std::bit_xor, std::mem_fn, std::map::value_comp, std::multimap::value_comp, std::function 또는 다른 std::not2 호출에서 얻은 함수 객체들은 이러한 타입을 정의하고 있어요. deprecated된 std::binary_function에서 파생된 함수 객체들도 마찬가지예요.

매개변수

pred - 이진 조건자

반환값

std::not2pred로 생성된 std::binary_negate<Predicate> 타입의 객체를 반환해요.

예제

#include <algorithm>
#include <cstddef>
#include <functional>
#include <iostream>
#include <vector>
 
struct old_same : std::binary_function<int, int, bool>
{
    bool operator()(int a, int b) const { return a == b; }
};
 
struct new_same
{
    bool operator()(int a, int b) const { return a == b; }
};

bool same_fn(int a, int b)
{
    return a == b;
}
 
int main()
{
    std::vector<int> v1{0, 1, 2};
    std::vector<int> v2{2, 1, 0};
    std::vector<bool> v3(v1.size());
 
    std::cout << "negating a binary_function:\n";
    std::transform(v1.begin(), v1.end(), v2.begin(), v3.begin(),
                   std::not2(old_same()));
 
    std::cout << std::boolalpha;
    for (std::size_t i = 0; i < v1.size(); ++i)
        std::cout << v1[i] << ' ' << v2[i] << ' ' << v3[i] << '\n';
 
    std::cout << "negating a standard functor:\n";
    std::transform(v1.begin(), v1.end(), v2.begin(), v3.begin(),
                   std::not2(std::equal_to<int>()));
 
    for (std::size_t i = 0; i < v1.size(); ++i)
        std::cout << v1[i] << ' ' << v2[i] << ' ' << v3[i] << '\n';
 
    std::cout << "negating a std::function:\n";
    std::transform(v1.begin(), v1.end(), v2.begin(), v3.begin(),
                   std::not2(std::function<bool(int, int)>(new_same())));
 
    for (std::size_t i = 0; i < v1.size(); ++i)
        std::cout << v1[i] << ' ' << v2[i] << ' ' << v3[i] << '\n';

    std::cout << "negating a std::reference_wrapper:\n";
    std::transform(v1.begin(), v1.end(), v2.begin(), v3.begin(),
                   std::not2(std::ref(same_fn)));
 
    for (std::size_t i = 0; i < v1.size(); ++i)
        std::cout << v1[i] << ' ' << v2[i] << ' ' << v3[i] << '\n';
}

출력:

negating a binary_function:
0 2 true
1 1 false
2 0 true
negating a standard functor:
0 2 true
1 1 false
2 0 true
negating a std::function:
0 2 true
1 1 false
2 0 true
negating a std::reference_wrapper:
0 2 true
1 1 false
2 0 true

같이 보기

함수 설명
not_fn (C++17) 보유한 함수 객체의 결과의 부정을 반환하는 함수 객체를 생성해요 (함수 템플릿)
binary_negate (C++17에서 deprecated, C++20에서 제거) 보유한 이진 조건자의 부정을 반환하는 래퍼 함수 객체예요 (클래스 템플릿)
function (C++11) 복사 생성 가능한 모든 호출 가능 객체의 복사 가능한 래퍼예요 (클래스 템플릿)
move_only_function (C++23) 주어진 호출 시그니처에서 한정자를 지원하는 모든 호출 가능 객체의 이동 전용 래퍼예요 (클래스 템플릿)
not1 (C++17에서 deprecated, C++20에서 제거) 사용자 정의 std::unary_negate 객체를 생성해요 (함수 템플릿)
ptr_fun (C++11에서 deprecated, C++17에서 제거) 함수 포인터로부터 어댑터 호환 함수 객체 래퍼를 생성해요 (함수 템플릿)
binary_function (C++11에서 deprecated, C++17에서 제거) 어댑터 호환 이진 함수 기본 클래스예요 (클래스 템플릿)

더 알아보기 (Learn more)

cppreference