1
2
标准库提供一组函数适配器,用来特殊化或者扩展一元和二元函数对象。常用适配器是:
1binder: binderCbinderbind1stbind2nd
2(negator) : negatorngeatornot1,not2
常用函数适配器列表如下:
bind1st(op, value)
bind2nd(op, value)
not1(op)
not2(op)
mem_fun_ref(op)
mem_fun(op)
ptr_fun(op)
3
#include"vector" #include"algorithm" #include"string" #include"functional" #include"iostream" using namespace std; class dayu { public: dayu(int i) { num = i; } bool operator()(int &n) { if (n > num) { return true; } else { return false; } } private: int num; }; int main01() { vector<int>v1(15); for (int i = 0; i < 15; i++) { v1[i] = rand() %100; } for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++) { cout << *it << " ";//41 67 34 0 69 24 78 58 62 64 5 45 81 27 61 } cout << endl; //求出5的个数;count /*template<class _InIt, class _Ty> inline typename iterator_traits<_InIt>::difference_type count(_InIt _First, _InIt _Last, const _Ty& _Val) { // count elements that match _Val _DEBUG_RANGE(_First, _Last); return (_Count_np(_Unchecked(_First), _Unchecked(_Last), _Val)); }*/ int num1 = count(v1.begin(),v1.end(),5); cout << "5的个数num1=: " << num1 << endl;//1 //////////////求出大于50的数的个数////////////////// //通过谓词的方式求解 /*template<class _InIt, class _Pr> inline typename iterator_traits<_InIt>::difference_type count_if(_InIt _First, _InIt _Last, _Pr _Pred) { // count elements satisfying _Pred _DEBUG_RANGE(_First, _Last); _DEBUG_POINTER(_Pred); return (_Count_if(_Unchecked(_First), _Unchecked(_Last), _Pred)); }*/ int num2 = count_if(v1.begin(),v1.end(),dayu(50)); cout << "dayu(50)num2=: " << num2 << endl;//8 //通过函数适配器的方式求解 /* // TEMPLATE STRUCT greater template<class _Ty = void> struct greater : public binary_function<_Ty, _Ty, bool> { // functor for operator> bool operator()(const _Ty& _Left, const _Ty& _Right) const { // apply operator> to operands return (_Left > _Right); } }; */ int num3 = count_if(v1.begin(),v1.end(),bind2nd(greater<int>(),50));//令_Right=50; cout << "大于50的个数num3= " << num3 << endl; //////////////////求奇偶数的个数(能否被2整除)/////////////////////// /* // TEMPLATE STRUCT modulus template<class _Ty = void> struct modulus : public binary_function<_Ty, _Ty, _Ty> { // functor for operator% _Ty operator()(const _Ty& _Left, const _Ty& _Right) const { // apply operator% to operands return (_Left % _Right); } }; */ //左参数来自容器,右参数来自自定义 int num4 = count_if(v1.begin(), v1.end(),bind2nd(modulus<int>(),2)); cout << "奇数个数num4= " << num4 << endl; //8 int num5 = count_if(v1.begin(), v1.end(), not1(bind2nd(modulus<int>(), 2))); cout << "偶数个数num5= " << num5 << endl; //7 return 0; } /////////////////////成员函数适配器/////////////// class Student {public: string num; string name; public: Student(string num,string name) { this->num = num; this->name = name; } bool show() { cout << num << ":"<<name << endl; return true; } }; int main02() { //mem_fun_ref 当集合是基于对象的 Student s1("1001","liubang"); Student s2("1002", "xiangyu"); vector<Student> v; v.push_back(s1); v.push_back(s2); for_each(v.begin(),v.end(),mem_fun_ref(&Student::show)); //mem_fun 当集合是基于对象指针的 Student *ps1=new Student("1001", "liubang"); Student *ps2=new Student("1002", "xiangyu"); vector<Student*> pv; pv.push_back(ps1); pv.push_back(ps2); for_each(pv.begin(), pv.end(), mem_fun(&Student::show)); return 0; } ///////////////////////普通函数适配器的基本用法(ptr_fun)////////////// /*template<class _Arg, class _Result> inline pointer_to_unary_function<_Arg, _Result, _Result (__cdecl *)(_Arg)> ptr_fun(_Result (__cdecl *_Left)(_Arg)) { // return pointer_to_unary_function functor adapter return (pointer_to_unary_function<_Arg, _Result, _Result (__cdecl *)(_Arg)>(_Left)); */ bool f(int x) { return x > 3; } bool g(int x,int y) { return x > y; } int main03() { int a[] = {0,1,2,3,4,5,6,7,8,9}; int nsize = sizeof(a) / sizeof(int); int numf3 = count_if(a, a + nsize, f); cout << "numf>3: "<<numf3 << endl; //6 对一个参数的普通函数而言,ptr_fun无太大优势; int numf4 = count_if(a, a + nsize, ptr_fun(f)); cout << "numptr_fun(f): " << numf4 << endl; //6 int numg = count_if(a, a + nsize, bind2nd(ptr_fun(g),5)); cout << "numptr_fun(g): " << numg << endl; //4 return 0; } int main() { //main01(); //main02(); main03(); system("pause"); return 0; }
文章来源: 函数适配器