仿函数、内建函数对象与lambda函数(匿名函数)

仿函数是指自定义的重载“()”的函数。

内建函数对象位于头文件<functional>中,常用的内建函数对象有:

①关系仿函数:

        template<class T> bool equal_to<T>                                    //等于

        template<class T> bool greater<T>              //大于

        template<class T> bool less<T>                //小于

②逻辑仿函数:

        template<class T> bool logical_and<T>           //逻辑与

        template<class T> bool logical_or<T>            //逻辑或

        template<class T> bool logical_not<T>           //逻辑非

匿名函数则由自己创建,格式为:

        [capture]          (parameters)              ->return-type                     {body} 

        捕获列表         参数列表(可省略)       返回值类型(可省略)           函数体

常用的需要用到仿函数的算法,在头文件<algorithm>中:

vector<int> v;
for (int i = 0; i < 10; i++) { v.push_back(i); }
//对该容器进行以下操作:v为 0 1 2 3 4 5 6 7 8 9

        ①遍历:for_each(只是遍历,根据输入仿函数的不同调用不同功能)

class shuchu {    //通过重载()进行输出
public:
	void operator()(int i) { cout << i << " "; }
};

for_each(v.begin(), v.end(), shuchu()); //对遍历到的元素进行输出。
cout << endl;
//通过调用匿名函数
for_each(v.begin(), v.end(), [](int i) {cout << i << " "; }); //
/*
    []                         //不捕获任何外部变量,捕获列表为空。
    (int i)       //参数列表。注意哦,这里是int而不是vector<int>::iterator,详情请看函数声明。
                               //不返回,所以为空。
    {cout << i << " "; }       // 函数体,实现输出数据的功能。
*/


//结果均为 0 1 2 3 4 5 6 7 8 9

         ②排序:sort(默认从小到大(升序)排序,若要按从大到小(降序)排序需要增加仿函数)

sort(v.begin(), v.end());     //默认升序

class jiangxu {
public:
	bool operator()(int v1, int v2) { return (v1 > v2); }
};
sort(v.begin(), v.end(), jiangxu());      //仿函数降序

sort(v.begin(), v.end(), greater<int>());       //内建函数对象降序

sort(v.begin(), v.end(), [](int v1, int v2)->bool {return (v1 < v2); }); //匿名函数升序

        ③寻找:find、find_if(查找自定义数据类型或者条件查找要调用仿函数)

//find函数,返回结束迭代器或者某个元素所在的位置。
vector<int>::iterator it = find(v.begin(), v.end(), 2); //找到返回该元素所在位置的迭代器

it = find(v.begin(), v.end(), 20); //未找到返回该容器的结束迭代器


//find_if函数,返回结束迭代器或者满足条件的第一个元素所在位置。
class greatfore {
public:
	bool operator()(int v) { return (v > 4); }
};
it = find_if(v.begin(), v.end(), greatfore()); //找到第一个大于4的元素所在位置

int x = 20;
it = find_if(v.begin(), v.end(), [x](int v)->bool {return (v > x); }); //未找到大于x的元素,返回结束迭代器。

总代码:

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

class shuchu {
public:
	void operator()(int i) { cout << i << " "; }
};

class jiangxu {
public:
	bool operator()(int v1, int v2) { return (v1 > v2); }
};

class greatfore {
public:
	bool operator()(int v) { return (v > 4); }
};

int main()
{
	vector<int> v;
	for (int i = 0; i < 10; i++) { v.push_back(i); }
	cout << "默认升序: ";
	sort(v.begin(), v.end());     //升序
	for_each(v.begin(), v.end(), shuchu()); //对遍历到的元素进行输出。
	cout << endl;
	cout << "仿函数降序:";
	sort(v.begin(), v.end(), jiangxu());
	for_each(v.begin(), v.end(), [](auto i) {cout << i << " "; }); //对遍历到的元素进行输出。
	cout << endl;
	cout << "内建函数对象降序:";
	sort(v.begin(), v.end(), greater<int>());
	for_each(v.begin(), v.end(), [](auto i) {cout << i << " "; }); //对遍历到的元素进行输出。
	cout << endl;
	cout << "匿名函数升序:";
	sort(v.begin(), v.end(), [](int v1, int v2)->bool {return (v1 < v2); });
	for_each(v.begin(), v.end(), [](auto i) {cout << i << " "; }); //对遍历到的元素进行输出。
	cout << endl;
	//寻找容器v中的某个数
	cout << "寻找元素 2:";
	vector<int>::iterator it = find(v.begin(), v.end(), 2); //找到返回该元素所在位置的迭代器
	(it != v.end()) ? cout << "找到元素" << *it << endl : cout << "未找到该元素" << endl;
	cout << "寻找元素 20:";
	it = find(v.begin(), v.end(), 20); //未找到返回该容器的结束迭代器
	(it != v.end()) ? cout << "找到元素" << *it << endl : cout << "未找到该元素" << endl;
	//寻找容器v中满足条件的某个数
	cout << "自定义谓词寻找第一个大于4的元素:";
	it = find_if(v.begin(), v.end(), greatfore()); 
	(it != v.end()) ? cout << "找到元素" << *it << endl : cout << "未找到大于4的元素" << endl;
	cout << "匿名函数寻找第一个大于x的元素:";
	int x = 20;
	it = find_if(v.begin(), v.end(), [x](int v)->bool {return (v > x); });
	(it != v.end()) ? cout << "找到元素" << *it << endl : cout << "未找到大于" << x << "的元素" << endl;
}

输出:

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值