STL(什麼是函數適配器)

在這裏插入圖片描述

函數適配器: 擴展函數的參數接口(假如函數有一個參數 再擴展一個接口 據可以傳遞兩個參數)

函數適配器

案例

//val 是for_each提供  tmp
//適配器2:公共繼承binary_function
//適配器3:參數的萃取
//適配器4:對operator()進行const修飾
class MyPrint:public binary_function<int,int, void>
{
public:
    void operator()(int val,int tmp) const
    {
        cout<<val+tmp<<" ";
    }
};

void test05()
{
    vector<int> v;
    v.push_back(10);
    v.push_back(20);
    v.push_back(30);
    v.push_back(40);
    v.push_back(50);

    //適配器1:bind2nd 或bind1st 綁定參數
    for_each(v.begin(), v.end(), bind2nd(MyPrint(),1000) );
    cout<<endl;
}

運行結果:
在這裏插入圖片描述

案例:bind2nd 或bind1st區別

bind2nd:講外界數據 綁定到第二個參數

bind1st:講外界數據 綁定到第一個參數

void test05()
{
    vector<int> v;
    v.push_back(10);
    v.push_back(20);
    v.push_back(30);
    v.push_back(40);
    v.push_back(50);

    //適配器1:bind2nd 或bind1st 綁定參數
    cout<<"bind2nd"<<endl;
    for_each(v.begin(), v.end(), bind2nd(MyPrint(),1000) );
    cout<<endl;

    cout<<"--------------------"<<endl;
    cout<<"bind1st"<<endl;
    for_each(v.begin(), v.end(), bind1st(MyPrint(),1000) );
    cout<<endl;
}

運行結果:
在這裏插入圖片描述
在這裏插入圖片描述

取反適配器(not1一元取反)

not1一元取反

not2二元取反

//取反適配器2:public unary_function
//取反適配器3:參數萃取
//取反適配器4:const修飾operator()
class MyGreaterThan3:public unary_function<int,bool>
{
public:
    //一元謂詞
    bool operator()(int val)const
    {
        return val>3;
    }
};
void test06()
{
    vector<int> v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    v.push_back(4);
    v.push_back(5);
    //找出第一個大於3的數
    vector<int>::iterator ret;
    ret = find_if(v.begin(),v.end(), MyGreaterThan3() );
    if(ret != v.end())
    {
        cout<<"*ret = "<<*ret<<endl;//4
    }

    //找出第一個小於3的數
    //取反適配器1:not1修飾
    ret = find_if(v.begin(),v.end(), not1(MyGreaterThan3()) );
    if(ret != v.end())
    {
        cout<<"*ret = "<<*ret<<endl;//4
    }
}

運行結果:
在這裏插入圖片描述

注意:

binary_function 二元繼承

unary_function 一元繼承

案例2:二元取反not2

class MyGreaterInt:public binary_function<int,int,bool>
{
public:
    bool operator ()(int v1,int v2)const
    {
        return v1>v2;
    }
};
void test07()
{
    vector<int> v;
    v.push_back(2);
    v.push_back(1);
    v.push_back(5);
    v.push_back(3);
    v.push_back(4);

    for_each(v.begin(),v.end(), [](int v){cout<<v<<" ";});
    cout<<endl;

    //默認小--->大
    //sort(v.begin(),v.end());
    //更改排序規則大-->小
    //sort(v.begin(),v.end(), MyGreaterInt());
    //使用not2對MyGreaterInt()取反  小--->大
    //sort(v.begin(),v.end(), not2(MyGreaterInt()));
    //使用not2對內建函數取反
    sort(v.begin(),v.end(), not2(greater<int>()));
    //sort(v.begin(),v.end(), less<int>());
    for_each(v.begin(),v.end(), [](int v){cout<<v<<" ";});
    cout<<endl;
}

運行結果:
在這裏插入圖片描述

成員函數適配器(mem_fun_ref)

class Person
{
public:
    string name;
    int age;
    Person(string name,int age)
    {
        this->name = name;
        this->age = age;
    }
    void showPerson()
    {
        cout<<"name = "<<this->name<<",age="<<this->age<<endl;
    }
};
void myPrintPerson(Person &ob)
{
    cout<<"name = "<<ob.name<<",age="<<ob.age<<endl;
}
void test08()
{
    vector<Person> v;
    v.push_back(Person("德瑪西亞",18));
    v.push_back(Person("狗頭",28));
    v.push_back(Person("牛頭",19));
    v.push_back(Person("小法",38));

    //遍歷 myPrintPerson普通函數
    //for_each(v.begin(),v.end(), myPrintPerson );
    //遍歷  Person成員函數
    //利用 mem_fun_ref 將Person內部成員函數適配
    for_each(v.begin(),v.end(), mem_fun_ref(&Person::showPerson) );
}

運行結果:
在這裏插入圖片描述

普通函數作爲適配器(ptr_fun)

//普通函數 作爲適配器
void myPrintInt01(int val,int tmp)
{
    cout<<val+tmp<<" ";
}
void test01()
{
    vector<int> v;
    v.push_back(10);
    v.push_back(20);
    v.push_back(30);
    v.push_back(40);

    //普通函數 需要使用ptr_fun轉換成函數適配器
    for_each(v.begin(),v.end(), bind2nd(ptr_fun(myPrintInt01),1000));
    cout<<endl;
}

運行結果;
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章