unary_negate Class
提供將指定的一元的函式傳回值的成員函式的樣板類別。
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
要變成相反值的一個項目的函式。_Left
要變成相反值的"一元" (Unary)函式的運算元。
傳回值
一元負運算的函式。
備註
樣板類別儲存一元的函式_Func*物件的複本。*它會定義其成員函式operator()所傳回! _Func (_Left)。
直接很少使用 unary_negate 建構函式。 Helper函式 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;
}
需求
標題: <functional>
命名空間: std