STL的学习之(二)适配器使用

一,STL的适配器

1,bind2nd语法的介绍:
  1. bind2nd() 函数, bind1sd函数
  2. 继承binary_function<参数1, 参数2,返回值类型 >写一个 伪函数要用const修饰
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;  //使用   标准的命名空间
#include <functional>
#include <iterator>
class Person
{
public:
    string m_Name;
    int m_age;
    Person(string name, int age) :m_Name(name), m_age(age) {}
    bool showclassint(Person p)
    {
        return this->m_age > p.m_age;
    }

};
class myComple :public binary_function<int, int, void>
{
public:
    void operator()(int val, int score) const
    {
        cout << val + score << endl;
    }


};

class Mycom
{
public:
    bool showclassint(int val1, int val2)
    {
        return val1 > val2;
    }
};
bool showint(int val1, int val2)
{
    return val1 > val2;
}


class mynot2 : public unary_function<int, void>
{
public:
    bool operator()(int val) const
    {
        return val < 5;
    }
};

void test01()
{
    vector<int> v;
    v.push_back(31);
    v.push_back(233);
    v.push_back(3232);
    v.push_back(5);


    //for_each(v.begin(), v.end(), not1(mynot2()));
    //for_each(v.begin(), v.end(), bind2nd(myComple(), 1000000));

    //sort(v.begin(), v.end(), showint);
    //sort(v.begin(), v.end(), mem_fun_ref(&Mycom::showclassint));

    //copy(v.begin(), v.end(), ostream_iterator<int>(cout << "   "));
    for_each(v.begin(), v.end(), [](int val) {cout << val << " "; });


}

void test05()
{
    vector<Person> v;
    v.push_back(Person("陈丽", 22));
    v.push_back(Person("王盼盼", 23));
    v.push_back(Person("王蓉", 21));
    v.push_back(Person("巍盼盼", 20));
    v.push_back(Person("杨艳", 25));


    sort(v.begin(), v.end(), mem_fun_ref(&Person::showclassint));


    for_each(v.begin(), v.end(), [](Person p) { cout << p.m_Name  << " "; });
}


int main(void)
{

    //test01();
    test05();
    system("pause");
    return 0;
}
取反适配器的使用not1,not2的使用要配合unary_function<参数类型, 返回值类型>接口使用

列子:

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
using namespace std;  //使用   标准的命名空间

//1,适配器的继承 binary_function类
class myCompare : public binary_function<int, int, void>
{
public:
    void operator()(int val, int score) const
    {
        cout << val + score << " ";
    }

};

//2,取反适配器的使用
class myFUNC : public unary_function<int, bool>
{
public:
    bool operator()(int val1) const
    {
        return (val1 > 5);
    }
};
//class myFUNC2 : public unary_function<int, int, bool>
//{
//public:
//  bool operator()(int val1, int socre) const
//  {
//      return (val1 > socre);
//  }
//};

void test01()
{
    vector<int> v;
    v.push_back(3);
    v.push_back(33);
    v.push_back(34);
    v.push_back(35);
    v.push_back(32);


    cout << "请输入的基值" << endl;
    int score;
    cin >> score;
    //绑定适配器 bind2nd
    //for_each(v.begin(), v.end(), bind2nd(myCompare(), score));
     for_each(v.begin(), v.end(), bind1st(myCompare(), score));
     cout << endl;
    vector<int>::iterator pos =  find_if(v.begin(), v.end(), not1(myFUNC()));
    pos = find_if(v.begin(), v.end(), not1(bind2nd(greater<int>(), 5)));
    if (pos != v.end())
    {
        cout << "pos" << endl;
    }


}


void myfunc(int val, int socre)
{
    cout << val  + socre << " ";
    cout << endl;
}

void test02()
{
    vector<int> v;
    v.push_back(3);
    v.push_back(33);
    v.push_back(34);
    v.push_back(35);
    v.push_back(32);


    for_each(v.begin(), v.end(), bind1st(ptr_fun (myfunc), 5));
}


class Person
{
public:
    string name;
    int age;
    Person(string name, int age) :name(name), age(age) {}

    void func()
    {
        cout << name << age + 5 << " ";
        cout << endl;
    }
};


void test03()
{
    vector<Person> v;
    v.push_back(Person("陈丽", 23));
    v.push_back(Person("王盼盼", 22));
    v.push_back(Person("王蓉", 13));

//使用成员函数要加上mem_fun_ref的 
    for_each(v.begin(), v.end(), mem_fun_ref(&Person::func));
}
int main(void)
{

    //test01();
    //test02();
    test03();
    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值