단항 및 이진 부정 자의 유용성은 쉽게 이해할 수 있습니다. 단항 부정 ( not1)와부정 부인 작성 방법?
는예 :
class Even
{
public:
bool operator() (const int& x) const { return x % 2 == 0; }
typedef int argument_type;
};
int values[] = { 9, 1, 8, 2, 7, 3, 6, 4, 5 };
int even = count_if(values, values + 9, Even());
int odd = count_if(values, values + 9, not1(Even())); // <= unary negator
cout << "We have " << even << " even elements in the array.\n";
cout << "We have " << odd << " odd elements in the array.\n";
출력 :
012,396,706,298,941,212,528,696,432 바이너리 부정 ( not2)와We have 4 even elements in the array.
We have 5 odd elements in the array.
예 10
출력 :
9 1 8 2 7 3 6 4 5
1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1
어떤 n 차 네게 이터 (not3, not4, not5 ... notn)에 대한?
이 아닌 요소의 수를 사이에 두 개의 수 (하한 한도 및 상한 한도)로 계산해야한다고 가정 해 봅시다.
.
int elems_betweem = count_if(values, values + n, not3(bind(Between<int>(), _1, lowerValue, upperValue)));
.
은 어떻게 not3 부정을 작성하려면 어떻게해야합니까?
더욱, 우리는 bind1st
및 bind2nd
대 bind
와 같은 방법으로 not1
및 not2
의 대용품으로 일반적인 not
을해야합니까?
여기에 'not3'이 필요 없습니다. - bind ((), _1, lowerValue, upperValue 사이)는 단항 연산자를 생성하므로 간단히'not1'이 필요합니다. –
Holt
C++ 17은 어떤 상황에서도 작동 할 수있는'std :: not_fn'을 소개합니다. – user2079303
예제에서는'not1'이 필요하고'not3'은 필요하지 않습니다. 'bind ((), _1, lowerValue, upperValue 사이)의 반환 값은 하나의 매개 변수를 취하고 3이 아닌 호출 가능한 호출입니다. –