C++:STL-函数对象

1.基本概念

  • 重载函数操作符的类,其对象叫函数对象
  • 函数对象使用重载的()时,行为类似函数调用,也叫仿函数
  • 是一个类不是函数
使用方式 :
  • 函数对象在使用时,可以像普通函数一样调用,可以有参数,可以有返回值
  • 函数对象超出普通函数的概念,可以有自己的状态
  • 函数对象可以作为参数传递
#include <iostream>
using namespace std;
#include <string>
//1.像普通函数一样,可以有参数,可以有返回值
class myAdd
{
public:
	int operator()(int v1, int v2)
	{
		return v1 + v2;
	}
};

void test01()
{
	myAdd myadd;
	cout<<myadd(10, 10);//直接输出结果
}

//2.超出普通函数概念,可以有自己的状态
class MyPrint
{
public:
	MyPrint()
	{
		this->count = 0;
	}
	void operator()(string test)
	{
		cout << test << endl;
		this->count++;
	}
	int count;//直接内部定义自己的状态
};
void test02()
{
	MyPrint myPrint;
	myPrint("世界你好");
	myPrint("世界你好");
	myPrint("世界你好");
	myPrint("世界你好");

	cout << "myprint调用次数:" << myPrint.count << endl;
}

//3.函数对象可以作为参数传递
void doPrint(MyPrint &mp, string test)
{
	mp(test);
}
void test03()
{
	MyPrint myPrint;
	doPrint(myPrint, "世界你好");
}
int main()
{
	//test01();
	//test02();
	test03();
	system("pause");
	return 0;
}

2.谓词

返回bool类型的仿函数

operator接受一个参数,一元谓词

operator接受两个参数,二元谓词

一元谓词例子

find_if(beg,end,class)

#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
//仿函数 返回值类型是bool数据类型,成为谓语
//一元谓词
class GreatFive
{
public:
	bool operator()(int val)
	{
		return val > 5;
	}
};
void test01()
{
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	//查找容器中是否有大于五的数字,返回迭代器,找不到返回end()迭代器
	//GreatFive()匿名函数对象
	vector<int>::iterator it=find_if(v.begin(), v.end(), GreatFive());
	if (it == v.end())
	{
		cout << "未找到" << endl;
	}
	else
	{
		cout << *it << endl;
	}
}



int main()
{
	test01();
	system("pause");
	return 0;
}
二元谓词
class myCompare
{
public:
	bool operator()(int v1,int v2)
	{
		return  v1> v2;
	}
};
void test01()
{
	vector<int> v;
	v.push_back(10);
	v.push_back(30);
	v.push_back(50);
	v.push_back(40);
	v.push_back(20);

	sort(v.begin(), v.end());
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
	//改变算法策略,改为大到小
	sort(v.begin(), v.end(), myCompare());

	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

3.内建函数对象-算数仿函数

需要引入头文件#include<functional>

  • template<class T> T plus<T>;//加法仿函数
  • template<class T> T minus<T>;//减法仿函数
  • template<class T> T multiplies<T>;//乘法仿函数
  • template<class T> T divides<T>;//除法仿函数
  • template<class T> T modulus<T>;//取模仿函数
  • template<class T> T negete<T>;//取反仿函数
void test01()
{
	negate<int>n;
	cout << n(50) << endl;
}

void test02()
{
	plus<int>p;
	cout << p(10, 20) << endl;

	minus<int>m;
	cout << m(30, 10) <<endl;
}

4.内建函数对象-关系仿函数

实现关系对比,但一定要写函数类型,如greater<int>();

  • template<class T> bool equal_to<T>;//等于
  • template<class T> bool not_equal_to<T>;//不等于
  • template<class T> bool greater<T>;//大于
  • template<class T>bool greater_euqal<T>;//大于等于
  • template<class T> bool less<T>;//小于
  • template<class T> bool less_euqal<T>;//小于等于
class myCompare
{
public:
	bool operator()(int v1, int v2)
	{
		return v1 > v2;
	}
};
void test01()
{
	vector<int> v;
	v.push_back(5);
	v.push_back(8);
	v.push_back(9);
	v.push_back(12);
	v.push_back(7);

	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}cout << endl;

	//降序,自己写的仿函数
	//sort(v.begin(), v.end(), myCompare());
	sort(v.begin(), v.end(), greater<int>());//编译器的比较对象,也实现从大到小排序
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}cout << endl;


}

5.内建函数对象-逻辑仿函数

实现逻辑运算,一定要写明参数类型

  • template<class T> bool logical_and<T>;//逻辑与
  • template<class T>bool logical_or<T>;//逻辑或
  • template<class T> bool logical_not<T>;//逻辑非

 

void test01()
{
	vector<bool>v;
	v.push_back(true);
	v.push_back(true);
	v.push_back(false);
	v.push_back(true);

	for (vector<bool>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
	//利用逻辑非将其搬运到v2,并执行取反操作
	vector<bool>v2;
	v2.resize(v.size());
	transform(v.begin(), v.end(),v2.begin(),logical_not<bool>());//将原容器搬运到现在的容器
	for (vector<bool>::iterator it = v2.begin(); it != v2.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值