STL算法总结1

一、适配器

1.函数对象适配

是什么?1.当函数参数不够用时,用来配置参数对象

              2.bind1st与bind2st----->将二元函数对象配置为一元函数对象           

              3.not1与not2------->取反

                4.普通函数适配

                5.成员函数适配mem_fun_ref)

                        MyPrintMaker是maker的成员函数

 

举个例子吧

#include<iostream>
#include<functional>
#include<algorithm>
#include<vector>

using namespace std;
struct func
{
	void operator()(int x)
	{
		cout << x << endl;
	}
};

int main()
{
	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(),func());
}

bind1st与bind2nd的例子

正常的遍历这样,但如果我要打印出的每一个都+100呢?

只要在cout那里加上就行,但这样是不是写死了,如果我换一个+200,是不是连func又要改,能不能加一个参数让要加的作为行参呢?但是两个参数for_each不能接受啊,怎么办呢?这个时候捆绑出手了,将两参数配成一个参数,怎么实现呢?

还是那串代码

#include<iostream>
#include<functional>
#include<algorithm>
#include<vector>

using namespace std;

//第一步:继承:public binary_function<int,int,void>
struct func:public binary_function<int,int,void>
{//第二步,加上const
    void operator()(int x,int y)const
    {//第三步,修改原代码
        cout << x+y << endl;
    }
};

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

//第四步,将func与100绑定给第一个参数
    for_each(v.begin(), v.end(),bind2nd(func(),100));
}

bind1st与bind2nd区别:前者将100绑定给第一个参数,后者为第二个

        这个是void类型加参数的做法

关于not1的例子

#include<iostream>
#include<functional>
#include<algorithm>
#include<vector>

using namespace std;

struct myless
	bool operator()(int x)
	{
		return x < 20;
	}
};
int main()
{
	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 it = find_if(v.begin(), v.end(), myless());
	cout << *it << endl;
}

这个是查找小于20的第一个迭代器--------->打印10

#include<iostream>
#include<functional>
#include<algorithm>
#include<vector>

using namespace std;
struct myless:public unary_function<int,bool>
{
    bool operator()(int x)const
    {
        return x < 20;
    }
};
int main()
{
    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 it = find_if(v.begin(), v.end(), not1(myless()));
    cout << *it << endl;
}

这个打印的确是20,其实就是取反,小于20变成大于等于20嘛

关于not2的例子

#include<iostream>
#include<functional>
#include<algorithm>
#include<vector>

using namespace std;
struct func:public binary_function<int,int,void>
{
    void operator()(int x,int y)const
    {
        cout << x+y << endl;
    }
};
int main()
{
    vector<int>v;
    v.push_back(10);
    v.push_back(20);
    v.push_back(30);
    v.push_back(40);
    v.push_back(50);
    sort(v.begin(), v.end(),not2(less<int>()));//我们可以看到,我在less前面加上not2,结果变成什么了呢?
    for_each(v.begin(), v.end(), bind2nd(func(), 0));
}

当然我们也可以自己打一个less仿函数

#include<iostream>
#include<functional>
#include<algorithm>
#include<vector>

using namespace std;
struct func:public binary_function<int,int,void>
{
    void operator()(int x,int y)const
    {
        cout << x+y << endl;
    }
};
struct myless:public unary_function<int,bool>
{
    
using first_argument_type = int;
    using second_argument_type = int;        //注意要加上这个

    bool operator()(int x, int y)const
    {
        return x < y;
    }

};
int main()
{
    vector<int>v;
    v.push_back(10);
    v.push_back(30);
    v.push_back(20);
    v.push_back(40);
    v.push_back(50);
    sort(v.begin(), v.end(),not2(myless()));
    for_each(v.begin(), v.end(), bind2nd(func(), 0));

 

上面都是仿函数的适配,那我们的普通函数怎么适配呢?按原来的做法必定报错

void func(int x)
{
    cout << x << " ";
}
int main()
{
    vector<int>v;
    v.push_back(10);
    v.push_back(30);
    v.push_back(20);
    v.push_back(40);
    v.push_back(50);

for_each(v.begin(), v.end(),func);
}

这个是正常的操作打印,我们想每一个加上一个数

第一步:将一个参数变成两个

void func(int x,int y)
{
    cout << x << " ";
}
int main()
{
    vector<int>v;
    v.push_back(10);
    v.push_back(30);
    v.push_back(20);
    v.push_back(40);
    v.push_back(50);

//第二步:加上绑定操作,将函数名变成函数对象

for_each(v.begin(), v.end(), bind2nd(ptr_fun(func), 0));
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值