37STL之函数适配器

来源:互联网 发布:hex转ascii算法 编辑:程序博客网 时间:2024/05/24 03:05

STL之函数适配器

  • STL之函数适配器
    • 理论知识
    • 常用函数适配器
    • 典型案例
    • 预定义函数对象和适配器案例代码

1.理论知识

2.常用函数适配器

标准库提供一组函数适配器,用来特殊化或者扩展一元和二元函数对象。常用适配器是:

1绑定器(binder):

binder通过把二元函数对象的一个实参绑定到一个特殊的值上,将其转换成一元函数对象。C++标准库提供两种预定义的binder适配器:bind1stbind2nd,前者把值绑定到二元函数对象的第一个实参上,后者绑定在第二个实参上。

2取反器(negator) :

negator是一个将函数对象的值翻转的函数适配器。标准库提供两个预定义的ngeator适配器:not1翻转预定义一元函数对象的真值,而not2翻转二元谓词函数的真值。

常用函数适配器列表如下

bind1st(op, value)bind2nd(op, value)not1(op)not2(op)mem_fun_ref(op)mem_fun(op)ptr_fun(op)

3.典型案例

class IsGreat{public:    IsGreat(int i)    {        m_num = i;    }    bool operator()(int &num)    {        if (num > m_num)        {            return true;        }        return false;    }protected:private:    int m_num;};void main43(){    vector<int>  v1;    for (int i=0; i<5; i++)    {        v1.push_back(i+1);    }    for (vector<int>::iterator it = v1.begin(); it!=v1.end(); it ++)    {        cout << *it << " " ;    }    int num1 = count(v1.begin(), v1.end(), 3);    cout << "num1:" << num1 << endl;    //通过谓词求大于2的个数    int num2 = count_if(v1.begin(), v1.end(), IsGreat(2));     cout << "num2:" << num2 << endl;    //通过预定义函数对象求大于2的个数   greater<int>() 有2个参数     //                                              param > 2    int num3 = count_if(v1.begin(), v1.end(), bind2nd(greater<int>(), 2 ) );    cout << "num3:" << num3 << endl;    //取模 能被2整除的数 求奇数    int num4 = count_if(v1.begin(), v1.end(), bind2nd(modulus <int>(), 2 ) );     cout << "奇数num4:" << num4 << endl;    int num5 = count_if(v1.begin(), v1.end(), not1( bind2nd(modulus <int>(), 2 ) ) );     cout << "偶数num5:" << num5 << endl;    return ;}

4.预定义函数对象和适配器案例代码

#include <iostream>using namespace std;#include "string"#include <vector>#include <list>#include "set"#include <algorithm>#include "functional"//plus<int> 预定义好的函数对象 能实现不同类型的数据的 + 运算//实现了 数据类型 和算法的分离 ===》通过函数对象技术实现的。。。。//思考:怎么样知道 plus<type> 是两个参数void main21(){    /*    template<class _Ty>    struct plus        : 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);        }    };    */    plus<int> intAdd;    int x = 10;     int y = 20;    int z = intAdd(x, y); // x + y     cout << "z:" << z << endl;    plus<string> stringAdd;    string s1 = "aaa";    string s2 = "bbb";    string s3 = stringAdd(s1, s2);    cout << "s3:" << s3 << endl;    vector<string> v1;    v1.push_back("bbb");    v1.push_back("aaa");    v1.push_back("ccc");    v1.push_back("zzz");    v1.push_back("ccc");    v1.push_back("ccc");    /*    template<class _Ty>    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);        }    };    */    sort(v1.begin(), v1.end(), greater<string>() );    for (vector<string>::iterator it=v1.begin(); it!=v1.end(); it++)    {        cout << *it << endl;    }    //求 ccc 出现的个数    string sc = "ccc";    //equal_to<string>() 有两个参数 left参数来自容器,right参数来自sc    //bind2nd函数适配器 :把预定义函数对象 和 第二个参数进行绑定    int num = count_if(v1.begin(), v1.end(), bind2nd(equal_to<string>(), sc) );    cout << "num: " << num << endl;}class IsGreat{public:    IsGreat(int i)    {        m_num = i;    }    bool operator()(int &num)    {        if (num > m_num)        {            return true;        }        return false;    }private:    int m_num;};void main22(){    vector<int> v1;    for (int i=0; i<10; i++)    {        v1.push_back(i+1);    }    for (vector<int>::iterator it=v1.begin(); it!=v1.end(); it++ )    {        cout << *it << " ";    }    cout << endl;    int num1 = count(v1.begin(), v1.end(), 3);    cout << "num1:" << num1 <<endl;    //通过 谓词 求大于2 的个数    int num2 = count_if(v1.begin(), v1.end(),  IsGreat(2));    cout << "num2:" << num2 <<endl;    /*    template<class _Ty>    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);        }    };    */    //通过 预定义的函数对象 求大于2 的个数    //greater<int>() 有两个参数 左参数来自容器的元素 ,右参数固定成2 (通过bind2nd做的)    int num3 = count_if(v1.begin(), v1.end(), bind2nd (greater<int>(), 2) );    cout << "num3:" << num3 <<endl;    //求 奇数的个数    int num4 = count_if(v1.begin(), v1.end(), bind2nd (modulus<int>(), 2) );    cout << "奇数的个数num4:" << num4 <<endl;    //求 偶数的个数 取反器(negator)     int num5 = count_if(v1.begin(), v1.end(), not1( bind2nd (modulus<int>(), 2) ) );    cout << "偶数的个数 num5:" << num5 <<endl;}void main2222(){    //main21();    main22(); //函数适配器综合案例    cout<<"hello..."<<endl;    system("pause");    return ;}
原创粉丝点击