unary_function Struct
定義型別可能會由衍生類別 (Derived Class) 會繼承提供的一元 (Unary) 函式物件的空的基本結構。
template<class Arg, class Result>
struct unary_function {
typedef Arg argument_type;
typedef Result result_type;
};
備註
範本結構做為基底型別 (Base Class) 定義表單 **result_typeoperator()**類別 (constargument_type&) const10% 的成員函式。
所有此類衍生的一元的函式可以在應用程式中的單一引數型別, argument_type 和傳回型別為 result_type。
範例
// functional_unary_function.cpp
// compile with: /EHsc
#include <vector>
#include <functional>
#include <algorithm>
#include <iostream>
using namespace std;
// Creation of a user-defined function object
// that inherits from the unary_function base class
class greaterthan10: unary_function<int, bool>
{
public:
result_type operator()(argument_type i)
{
return (result_type)(i > 10);
}
};
int main()
{
vector<int> v1;
vector<int>::iterator Iter;
int i;
for (i = 0; i <= 5; 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;
result1 = count_if(v1.begin(), v1.end(), greaterthan10());
cout << "The number of elements in v1 greater than 10 is: "
<< result1 << "." << endl;
}
需求
標題: <functional>
命名空間: std