functional_binary_negate
functional_binary_negate (이진 부정 함수 객체)
std::binary_negate는 보관하고 있는 이진 조건자의 부정을 반환하는 래퍼 함수 객체예요. 이 객체를 사용하면 조건식을 뒤집어야 할 때 별도의 함수 객체를 새로 정의하지 않아도 돼요. 다만 C++17에서 deprecated로 지정되었고 C++20에서 제거되었어요.
출처: cppreference
본문
std::binary_negate는 보관하고 있는 이진 조건자의 부정을 반환하는 래퍼 함수 객체예요.
이진 조건자 타입은 조건자의 매개변수 타입으로 변환 가능한 두 개의 멤버 타입 first_argument_type과 second_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에서 파생된 함수 객체도 마찬가지예요.
std::binary_negate 객체는 헬퍼 함수 std::not2로 쉽게 생성할 수 있어요.
멤버 타입
| 타입 | 정의 |
|---|---|
| first_argument_type | Predicate::first_argument_type |
| second_argument_type | Predicate::second_argument_type |
| result_type | bool |
멤버 함수
| 함수 | 설명 |
|---|---|
| (constructor) | 보관된 조건자를 사용해 새로운 binary_negate 객체를 생성해요 (public 멤버 함수) |
| operator() | 보관된 조건자를 호출한 결과의 논리적 부정을 반환해요 (public 멤버 함수) |
std::binary_negate::binary_negate
| explicit binary_negate ( Predicate const & pred ); | (constexpr since C++14) |
|---|
보관할 조건자 pred를 사용해 std::binary_negate 함수 객체를 생성해요.
매개변수
| pred | - | 조건자 함수 객체 |
std::binary_negate::operator()
| bool operator ()( first_argument_type const & x , second_argument_type const & y ) const ; | (constexpr since C++14) |
|---|
pred(x, y)를 호출한 결과의 논리적 부정을 반환해요.
매개변수
| x | - | 조건자에 전달할 첫 번째 인자 | | y | - | 조건자에 전달할 두 번째 인자 |
반환값
pred(x, y)를 호출한 결과의 논리적 부정.
예제
#include <algorithm>
#include <cstddef>
#include <functional>
#include <iostream>
#include <vector>
struct same : std::binary_function<int, int, bool>
{
bool operator()(int a, int b) const { return a == b; }
};
int main()
{
std::vector<int> v1;
for (int i = 0; i < 7; ++i)
v1.push_back(i);
std::vector<int> v2(v1.size());
std::reverse_copy(v1.begin(), v1.end(), v2.begin());
std::vector<bool> v3(v1.size());
std::binary_negate<same> not_same((same()));
// C++11 solution:
// std::function<bool (int, int)> not_same =
// [](int x, int y) -> bool { return !same()(x, y); };
std::transform(v1.begin(), v1.end(), v2.begin(), v3.begin(), not_same);
std::cout.setf(std::ios_base::boolalpha);
for (std::size_t i = 0; i != v1.size(); ++i)
std::cout << v1[i] << " != " << v2[i] << " : " << v3[i] << '\n';
}
출력:
0 != 6 : true
1 != 5 : true
2 != 4 : true
3 != 3 : false
4 != 2 : true
5 != 1 : true
6 != 0 : true
같이 보기
| binary_function (deprecated in C++11) (removed in C++17) | 어댑터 호환 이진 함수 기본 클래스 (클래스 템플릿) [edit] |
|---|---|
| function (C++11) | 복사 생성 가능한 호출 가능 객체를 담는 복사 가능한 래퍼 (클래스 템플릿) [edit] |
| move_only_function (C++23) | 주어진 호출 시그니처에서 한정사를 지원하는 호출 가능 객체를 담는 이동 전용 래퍼 (클래스 템플릿) [edit] |
| not2 (deprecated in C++17) (removed in C++20) | 사용자 지정 std::binary_negate 객체를 생성해요 (함수 템플릿) [edit] |
| ptr_fun (deprecated in C++11) (removed in C++17) | 함수 포인터로부터 어댑터 호환 함수 객체 래퍼를 만들어요 (함수 템플릿) [edit] |
| unary_negate (deprecated in C++17) (removed in C++20) | 보관하고 있는 단항 조건자의 부정을 반환하는 래퍼 함수 객체 (클래스 템플릿) [edit] |