第十二天之预定义函数对象和函数适配器

预定义函数对象

1)预定义函数对象基本概念:标准模板库 STL 提前定义了很多预定义函数对象,#include 必须包含

//1 使用预定义函数对象:
//类模板 plus<> 的实现了: 不同类型的数据进行加法运算
void main41()
{
plus<int> intAdd;
int x = 10;
int y = 20;
int z = intAdd(x, y); //等价于 x + y 
cout << z << endl;
plus<string> stringAdd;
string myc = stringAdd("aaa", "bbb");
cout << myc << endl;
vector<string> v1;
v1.push_back("bbb");
v1.push_back("aaa");
v1.push_back("ccc");
v1.push_back("zzzz");
//缺省情况下,sort()用底层元素类型的小于操作符以升序排列容器的元素。
//为了降序,可以传递预定义的类模板 greater,它调用底层元素类型的大于操作符:
cout << "sort()函数排序" << endl;;
sort(v1.begin(), v1.end(), greater<string>() ); //从大到小
for (vector<string>::iterator it=v1.begin(); it!=v1.end(); it++ )
{
cout << *it << endl;
} }

2)算术函数对象


预定义的函数对象支持加、减、乘、除、求余和取反。调用的操作符是与 type 相关联的实
例
加法:plus<Types>
plus<string> stringAdd;
sres = stringAdd(sva1,sva2);

减法:minus<Types>
乘法:multiplies<Types>
除法 divides<Tpye>
求余:modulus<Tpye>
取反:negate<Type>
negate<int> intNegate;
ires = intNegate(ires);
Ires= UnaryFunc(negate<int>(),Ival1);


3)关系函数对象
等于 equal_to
equal_to stringEqual;
sres = stringEqual(sval1,sval2);
不等于 not_equal_to
大于 greater
大于等于 greater_equal
小于 less
小于等于 less_equal
void main42()
{
vector v1;
v1.push_back(“bbb”);
v1.push_back(“aaa”);
v1.push_back(“ccc”);
v1.push_back(“zzzz”);
v1.push_back(“ccc”);
string s1 = “ccc”;
//int num = count_if(v1.begin(),v1.end(), equal_to(),s1);
int num = count_if(v1.begin(),v1.end(),bind2nd(equal_to(), s1));
cout << num << endl;
}

4)逻辑函数对象
逻辑与 logical_and
logical_and indAnd;
ires = intAnd(ival1,ival2);
dres=BinaryFunc( logical_and(),dval1,dval2);
逻辑或 logical_or
逻辑非 logical_not
logical_not IntNot;
Ires = IntNot(ival1);
Dres=UnaryFunc( logical_not,dval1);

函数适配器

1)函数适配器的理论知识
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
2)常用函数函数适配器
标准库提供一组函数适配器,用来特殊化或者扩展一元和二元函数对象。常用适配器是: 1 绑定器(binder): binder 通过把二元函数对象的一个实参绑定到一个特殊的值上,将其转
换成一元函数对象。C++标准库提供两种预定义的 binder 适配器:bind1st 和 bind2nd,前
者把值绑定到二元函数对象的第一个实参上,后者绑定在第二个实参上。
2 取反器(negator) : negator 是一个将函数对象的值翻转的函数适配器。标准库提供两个预定
义的 ngeator 适配器:not1 翻转一元预定义函数对象的真值,而 not2 翻转二元谓词函数的真
值。
常用函数适配器列表如下:
bind1st(op, value)
bind2nd(op, value)
not1(op)
not2(op)
mem_fun_ref(op)
mem_fun(op)
ptr_fun(op

3)常用函数适配器案例

class IsGreat
{
public:
IsGreat(int i)
{
m_num = i;
}
bool operator()(int &num)
{
if (num > m_num)
{
return true;
}
return false;
}
protected:
private:
int m_num;
};
void main43()
{
vector<int> v1;
for (int i=0; i<5; i++)
{
v1.push_back(i+1);
}
for (vector<int>::iterator it = v1.begin(); it!=v1.end(); it ++)
{
cout << *it << " " ;
}
int num1 = count(v1.begin(), v1.end(), 3);
cout << "num1:" << num1 << endl;
//通过谓词求大于 2 的个数
int num2 = count_if(v1.begin(), v1.end(), IsGreat(2)); 
cout << "num2:" << num2 << endl;
//通过预定义函数对象求大于 2 的个数 greater<int>() 有 2 个参数
// param > 2
int num3 = count_if(v1.begin(), v1.end(), bind2nd(greater<int>(), 2 ) );
cout << "num3:" << num3 << endl;
//取模 能被 2 整除的数 求奇数
int num4 = count_if(v1.begin(), v1.end(), bind2nd(modulus <int>(), 2 ) ); 
cout << "奇数 num4:" << num4 << endl;
int num5 = count_if(v1.begin(), v1.end(), not1( bind2nd(modulus <int>(), 2 ) ) ); 
cout << "偶数 num5:" << num5 << endl;
return ;
}

综合案例:


#include<iostream>
using namespace std;
#include "list"
#include "vector"
#include "string"
#include "algorithm"
#include "set"
#include "functional"

//预定义好的函数对象,实现了不同类型的数据的 + 操作
//实现了数据类型和算法的分离 ==》通过函数对象实现

//思考:怎么样知道plus<int>是两个参数
void main21()
{
	plus<int> intadd;
	int x = 10;
	int y = 20;
	int z = intadd(x, y);	// z = x +y
	cout << "z : " << z << endl;

	plus<string> stringadd;
	string s1 = "aaa";
	string s2 = "bbb";
	string s3 = stringadd(s1, s2);
	cout << "s3 : " << s3 << endl;

	vector<string> v1;
	v1.push_back("aaa");
	v1.push_back("bbb");
	v1.push_back("ccc");
	v1.push_back("ddd");

	sort(v1.begin(), v1.end(),greater<string>());	//从大到小排序
	for (vector<string>::iterator it = v1.begin(); it != v1.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;

	//求ccc出现的次数
	string sc = "ccc";
	//函数适配器bind2nd:将预定义函数对象和第二个参数绑定
	//equal_to<string>() 有两个参数 左left参数来自容器,右right参数来自sc
	int num = count_if(v1.begin(), v1.end(), bind2nd(equal_to<string>(), sc));
	cout << "num : " << num << endl;

}

class IsGreat
{
public:
	IsGreat(int i)
	{
		m_num = i;
	}
	bool operator()(int &num)
	{
		if (num > m_num)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
private:
	int m_num;
};

void main22()
{
	vector<int> v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i+1);
	}
	for (vector<int>::iterator it = v1.begin();it != v1.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
	int num = count(v1.begin(), v1.end(),3);
	cout << "num: " << num << endl;
	//通过谓词 求大于2的个数
	int num2 = count_if(v1.begin(), v1.end(),IsGreat(2));	//2赋值给m_num,之后会自动调用()重载函数,将容器的每一个值赋值给num与m_num比较,若大于m_num返回true
	cout << "num2: " << num2 << endl;

	//通过预定义的函数对象 求大于2的个数
	//greater<int>() 有两个参数 左left参数来自容器,右right参数固定成2
	int num3 = count_if(v1.begin(), v1.end(), bind2nd(greater<int>(), 2));	//2赋值给m_num,之后会自动调用()重载函数,将容器的每一个值赋值给num与m_num比较,若大于m_num返回true
	cout << "num3: " << num3 << endl;

	//求奇数的个数
	int num4 = count_if(v1.begin(), v1.end(), bind2nd(modulus<int>(), 2));	
	cout << "奇数的个数num4: " << num4 << endl;

	//求偶数的个数
	int num5 = count_if(v1.begin(), v1.end(), not1(bind2nd(modulus<int>(), 2)));	
	cout << "偶数的个数num5: " << num5 << endl;

}
void main()
{
	//main21();
	main22(); //函数适配器综合案例
	system("pause");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值