STL-algorithm

谓词

  • 返回值类型为bool的普通函数或仿函数 就叫谓词。
  • 如果普通函数或仿函数 有一个参数 就叫一元谓词。
  • 如果普通函数或仿函数 有二个参数 就叫二元谓词。

普通函数作为一元谓词

#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;

bool greaterThan20(int val)
{
    return val>20;
}

void test01()//一元谓词,普通函数
{
    vector<int>v;
    v.push_back(10);
    v.push_back(20);
    v.push_back(30);
    v.push_back(40);
    v.push_back(50);
    vector<int>::iterator ret;
    for_each(v.begin(),v.end(),[](int val){
    cout<<val<<" ";});
    cout<<endl;
    //普通函数
    ret=find_if(v.begin(),v.end(),greaterThan20);

    if(ret!=v.end())
    {
        cout<<"第一个大于20的数为:"<<*ret<<endl;
    }
}

仿函数做一元谓词

class MygreaterThan20
{
public:
    bool operator ()(int val)
    {
        return val>20;
    }
};
void test02()//一元谓词,仿函数
{
    vector<int>v;
    v.push_back(10);
    v.push_back(20);
    v.push_back(30);
    v.push_back(40);
    v.push_back(50);

    for_each(v.begin(),v.end(),[](int val){
    cout<<val<<" ";});
    cout<<endl;
    vector<int>::iterator ret;
    ret= find_if(v.begin(),v.end(),MygreaterThan20());
    cout<<"第一个大于20的数为:"<<*ret<<endl;
}

二元谓词【普通函数】【仿函数】

bool myGreater(int v1,int v2)
{
    return v1<v2;
}
class Mygreater
{
public:
    bool operator()(int v1,int v2) {
        return v1<v2;
    }
};
void test03()//二元谓词
{
    vector<int>v;
    v.push_back(20);
    v.push_back(30);
    v.push_back(50);
    v.push_back(40);
    v.push_back(10);

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

    //sort(v.begin(),v.end());//默认
    //sort(v.begin(),v.end(),myGreater);//普通函数
    sort(v.begin(),v.end(),Mygreater());//仿函数
    for_each(v.begin(),v.end(),[](int val){cout<<val<<" ";});
}

内建函数对象

STL 内建了一些函数对象。分为:算数类函数对象,关系运算类函数对象,逻辑运算 类仿函数。这些仿函数所产生的对象,用法和一般函数完全相同,当然我们还可以 产生无名的临时对象来履行函数功能。

void test04()//内建函数对象
{
    vector<int>v;
    v.push_back(20);
    v.push_back(30);
    v.push_back(50);
    v.push_back(40);
    v.push_back(10);

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

    sort(v.begin(),v.end(),greater<int>());
    for_each(v.begin(),v.end(),[](int val){cout<<val<<" ";});

}

函数适配器

bind2nd 或bind1st区别

void test05()//函数适配器[bind2nd 或bind1st区别]
{
    vector<int>v;
    v.push_back(20);
    v.push_back(30);
    v.push_back(50);
    v.push_back(40);
    v.push_back(10);

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

取反适配器

unary_function 一元继承

class MygreaterThan3:public unary_function<int ,bool>
{
public:
    bool operator ()(int val)const
    {
        return val>3;
    }

};
test06()//取反适配器【 unary_function 一元继承】
{
    vector<int>v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    v.push_back(4);
    v.push_back(5);
    vector<int>::iterator ret;
    ret=find_if(v.begin(),v.end(),MygreaterThan3());
    if(ret!=v.end())
    {
        cout<<*ret<<endl;
    }
    cout<<"取反之后"<<endl;
    ret=find_if(v.begin(),v.end(),not1(MygreaterThan3()));
    if(ret!=v.end())
    {
        cout<<*ret<<endl;
    }
}

binary_function 二元继承

class MygreatInt:public binary_function<int,int,bool>
{
public:
   bool operator ()(int v1,int v2)const
   {
       return v1<v2;
   }
};
test07()//取反适配器【binary_function 二元继承 】
{
    vector<int>v;
    v.push_back(2);
    v.push_back(4);
    v.push_back(5);
    v.push_back(1);
    v.push_back(3);
    for_each(v.begin(),v.end(),[](int val){cout<<val<<" ";});
    cout<<endl;

    //sort(v.begin(),v.end());
    sort(v.begin(),v.end(),MygreatInt());
    sort(v.begin(),v.end(),not2(MygreatInt()));
     for_each(v.begin(),v.end(),[](int val){cout<<val<<" ";});

}

成员函数适配器

  • 如果容器存放的是对象指针, 那么用 mem_fun
  • 如果容器中存放的是对象实体,那么用 mem_fun_ref
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));
   // for_each(v.begin(),v.end(),myPrintPerson);
    for_each(v.begin(),v.end(),mem_fun_ref(&Person::showPerson));
}
int main(int argc, char *argv[])
{
    //test01();
    //test02();
      //test03();
    //test04();
      // test05();
    //test06();
    //test07();
    test08();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值