STL:bind1st、bind2nd绑定适配器、not、unary_function与binary_function区别、find_if()使用

本文详细介绍了C++中的函数对象适配器,包括bind1st、bind2nd用于绑定参数,not1和not2用于取反,以及ptr_fun和mem_fun系列适配器将普通函数和成员函数转换为函数对象。通过示例展示了如何使用这些适配器进行函数操作,如在容器中遍历和排序。
摘要由CSDN通过智能技术生成

函数对象适配器

函数对象适配器是完成一些配接工作,这些配接包括绑定(bind)、否定(negate),以及对一般函数或成员函数的修饰,使其成为函数对象。

bind1st:将参数绑定为函数对象的第一个参数
bind2nd:将参数绑定为函数对象的第二个参数
not1:对一元函数对象取反
not2:对二元函数对象取反

函数对象适配器:
ptr_fun:将普通函数修饰成函数对象
成员函数适配器:
mem_fun:修饰成员函数
mem_fun_ref:修饰成员函数

1.绑定适配器:将一个二元函数对象转变为一元函数对象

bind1st

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
struct MyPrint:public binary_function<int,int,void>
//binary_function,int、int为operator参数,void为返回值
{
	void operator()(int v,int val) const{//const防止修改本值
		cout << "v=" << v << ",val=" << val ;
		cout <<",v+val=" << v + val << " " << endl;
	}
};
void test01() {
	vector<int> v;
	for (int i = 0; i < 10; i++) {
		v.push_back(i);
	}
	for_each(v.begin(), v.end(), bind1st(MyPrint(),100));//bind1st
}

在这里插入图片描述

bind2nd

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
struct MyPrint:public binary_function<int,int,void>
{
	void operator()(int v,int val) const{
		cout << "v=" << v << ",val=" << val ;
		cout <<",v+val=" << v + val << " " << endl;
	}
};
void test01() {
	vector<int> v;
	for (int i = 0; i < 10; i++) {
		v.push_back(i);
	}
	for_each(v.begin(), v.end(), bind2nd(MyPrint(),100));//bind2nd
}

在这里插入图片描述

2.取反适配器:not

struct Mycompare:public binary_function<int,int,bool>//binary_function
{
	bool operator()(int v1, int v2) const{
		return v1 > v2;
	}
};
struct MyPrint01 
{
	void operator()(int v)  {
		cout << v <<" ";
	}
};
void test02() {
	vector<int> v;
	for (int i = 0; i < 10; i++) {
		v.push_back(rand()%100);
	}
	for_each(v.begin(), v.end(), MyPrint01());
	cout << endl;
	sort(v.begin(), v.end(), Mycompare());
	for_each(v.begin(), v.end(), MyPrint01());
	cout << endl;
	sort(v.begin(), v.end(), not2(Mycompare()));//not2
	for_each(v.begin(), v.end(), MyPrint01());
}

在这里插入图片描述

3.unary_function与binary_function区别、find_if()使用

struct MyPrint01 
{
	void operator()(int v)  {
		cout << v <<" ";
	}
};
struct MyGreater5:public unary_function<int,bool>//unary_function,接受一元参数
{
	bool operator()(int val) const{
		return val > 5;//查找大于5的迭代器
	}
};
void test02() {
	vector<int> v;
	for (int i = 0; i < 10; i++) {
		v.push_back(i);
	}
	for_each(v.begin(), v.end(), MyPrint01());
	cout << endl;
	vector<int>::iterator it= find_if(v.begin(), v.end(), MyGreater5());//find_if查找,返回迭代器
	if (it == v.end()) {
		cout << "不存在!" << endl;
	}
	else {
		cout << *it << endl;
	}
}

在这里插入图片描述

4.ptr_fun 将普通函数 转为 函数对象

普通函数:
void Myprint(int val) {
cout << val << " ";
}
void test01() {
vector v;
for (int i = 0; i < 10; i++)
v.push_back(i);
for_each(v.begin(), v.end(), Myprint);
}
在这里插入图片描述
使用ptr_fun,转为函数对象:

void Myprint(int val,int n) {
	cout << val+n << " ";
}
void test01() {
	vector<int> v;
	for (int i = 0; i < 10; i++)
		v.push_back(i);
	for_each(v.begin(), v.end(), bind2nd(ptr_fun(Myprint),10));
}

在这里插入图片描述

5.mem_fun_ref

如果容器中存放的对象或对象指针,我们用for_each算法打印的时候,调用类自己提供的打印函数。

class Person {
public:
	Person(int id, int age):id(id),age(age) {}
	void Show() {
		cout << "age:" << this->age << ",id:" << this->id << endl;
	}
	int id;
	int age;
};
void test02() {
	vector<Person> v;
	Person p1(1, 2), p2(3, 4), p3(5, 6);
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	//调用Person成员函数
	for_each(v.begin(), v.end(), mem_fun_ref(&Person::Show));
}

在这里插入图片描述

6.mem_fun

class Person {
public:
	Person(int id, int age):id(id),age(age) {}
	void Show() {
		cout << "age:" << this->age << ",id:" << this->id << endl;
	}
	int id;
	int age;
};
void test03() {
	vector<Person*> v;
	Person p1(1, 2), p2(3, 4), p3(5, 6);
	v.push_back(&p1);
	v.push_back(&p2);
	v.push_back(&p3);
	for_each(v.begin(), v.end(), mem_fun(&Person::Show));
}

7.mem_fun和mem_fun_ref区别

如果存放对象指针,使用mem_fun;
如果存放对象,使用mem_fun_ref;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值