STL学习3常用算法3.2适配器

本文深入探讨了C++标准库中的函数对象适配器,包括bind2nd、bind1st、not1、not2、ptr_fun及成员函数适配器的使用方法与应用场景。通过具体实例展示了如何利用这些适配器来调整函数对象的行为,实现更灵活的算法设计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、函数对象适配器

    1.1 bind2ndz或者bind1st  将两个参数进行绑定

       1.1.1 bind2nd  将参数绑定为函数对象的第二个参数

        1.1.2 bind1st的参数绑定是相反的 将参数绑定为函数对象的第一个参数

   1.2  继承 public binary_function<类型1,类型2,返回值>

    1.3  加const

2、取反适配器

     2.1.1 一元取反  not1

          2.1.1.1  继承 unary_function<类型1,返回值类型>

    2.1.2  二元取反 not2

3  函数指针适配器

      3.1 ptr_fun  将函数指针(普通函数)适配为函数对象  用于将普通函数用于需要函数对象处

4 成员函数适配器

   4.1 men_fun_ref  用于容器中存放对象本体

   4.2 men_fun 用于容器中存放对象指针 

5 测试程序

#include"pch.h"
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<string>
using namespace std;

//1、函数对象适配器

class myPrint:public binary_function<int,int,void>
{
public:
	void operator()(int val,int start)const
	{
		cout << "val= "<<val
			<<" strat= "<<start
			<<" sum= "<<val+start << endl;
	}
};
void test01()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	//for_each第三个参数只接受传递一个参数的函数对象
	//若函数对象需要两个参数 使用内建函数对象 
	/*使用步骤:1、包含functional头文件  将参数bind2nd进行绑定
	            2、做继承 public binary_function<参数1,参数2,返回值>
				3、重载()函数后加const
				*/
	int num;
	cout << "请输入起始值:";
	cin >> num;
	//bind2nd  将参数绑定为函数对象的第二个参数
	for_each(v.begin(), v.end(),bind2nd(myPrint(),num) );
	//bind1st的参数绑定是相反的 将参数绑定为函数对象的第一个参数
	//for_each(v.begin(), v.end(), bind1st(myPrint(), num));
	
}

//取反适配器
class GreaterThen5:public unary_function<int,bool>
{
public:
	bool operator()(int val)const
	{
		return val > 5;
	}
};
void test02()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	vector<int>::iterator pos;
	pos=find_if(v.begin(), v.end(), GreaterThen5());
	
	if (pos != v.end())
	{
		cout << "找到大于5的数字:" << *pos << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}

	//取反适配器使用
	//方法一 更改自定义函数对象
	/* 1、一元取反 not1
	   2、继承public unary_function<类型1,返回值>
	   3、加const*/
	pos = find_if(v.begin(), v.end(),not1( GreaterThen5()));
	//方法二 全部采用内建函数
	//pos = find_if(v.begin(), v.end(), not1(bind2nd(greater<int>(),5)));
	if (pos != v.end())
	{
		cout << "找到第一个小于5的数字:" << *pos << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}
}

//函数指针适配器
void myPrint3(int val,int start)
{
	cout << val+start << endl;
}
void test03()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	int num;
	cout << "请输入起始值:";
	cin >> num;
	//函数指针适配器将普通函数指针  适配成函数对象
	//ptr_fun
	for_each(v.begin(), v.end(),bind2nd(ptr_fun(myPrint3),num));
}

//成员函数适配器
class Person
{
public:
	Person(string name, int age)
	{
		m_Name = name;
		m_Age = age;
	} 
	void print()const
	{
		cout << "姓名:" << m_Name << " 年龄:" << m_Age << endl;
	}
	string m_Name;
	int m_Age;

};

void test04()
{
	vector<Person>v;
	Person p1("aaa", 10);
	Person p2("bbb", 20);
	Person p3("ccc", 30);
	Person p4("ddd", 40);
	Person p5("eee", 50);
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);
	for_each(v.begin(), v.end(), mem_fun_ref(&Person::print));
	//如果容器中存放的对象指针用mem_fun
}
int main()
{
	//test01();
	//test02();
	//test03();
	test04();
}

    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值