C++学习笔记——STL【函数对象】

函数对象

函数对象的概念

概念:

  • 重载函数调用操作符的类,其对象常称为函数对象
  • 函数对象使用重载的()时,行为类似函数调用,也叫仿函数

本质:

函数对象(仿函数)是一个,不是一个函数

函数对象使用

特点:

  • 函数对象在使用时,可以像普通函数那样调用, 可以有参数,可以有返回值
  • 函数对象超出普通函数的概念,函数对象可以有自己的状态
  • 函数对象可以作为参数传递
#include<iostream>
#include<string>

using namespace std;

// 重载了函数调用操作符() 的类称为函数对象,又叫做仿函数
class MyAdd {

public:
	// 函数对象可以有自己的状态
	int count;
	MyAdd() {
		count = 0;
	}
	// 像普通函数一样,可以有参数、返回值
	int operator()(int x, int y) {
		// 每次被调用时,都更新自身状态,调用次数加一
		count++;
		return x + y;
	}
};
// 以引用方式传入函数对象参数
void dosome(MyAdd& add, int a, int b) {
	cout << add(a, b) << endl;
}

int main() {
	// 实例化函数对象
	MyAdd myadd;
	// 函数对象的调用就行普通函数调用
	int result = myadd(2,5);
	cout << result << endl;
	cout << "函数对象被调用的次数:" << myadd.count << endl; // 1
	// 函数对象可以作为参数进行传递
	dosome(myadd, 6, 9);
	cout << "函数对象被调用的次数:" << myadd.count << endl; // 2
	return 0;
}

谓词

谓词的概念

概念:

  • 返回bool类型的仿函数称为谓词
  • 如果operator()接受一个参数,那么叫做一元谓词
  • 如果operator()接受两个参数,那么叫做二元谓词

一元谓词

#应用案例:一元谓词作为 find_if 函数的查找条件

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>

using namespace std;

/*谓词:返回值类型为 bool 的仿函数*/

/*一元谓词:函数调用操作符重载时只接收一个参数的谓词称为一元谓词*/
/*以函数对象的形式定义一个一元谓词, find_if() 函数的查找规则*/
class check_val {
public:
	// 判断目标值是否为0
	bool operator()(int n) {
		return n == 0;
	}
};

void print_int(int n) {
	cout << n << endl;
}

int main() {
	// 随机种子
	srand((unsigned int)time(NULL));
	vector<int>v;
	for (int i = 0; i < 5; i++) {
		// 生成 -5~5之间的随机整数
		int n = (rand() % (5 + 5 + 1)) - 5;
		v.push_back(n);
	}
	for_each(v.begin(), v.end(), print_int);
	// 按条件查找函数:使用一元谓词check_val作为查找条件,查找值为 0 的元素
	vector<int>::iterator p = find_if(v.begin(), v.end(), check_val());
	if (p != v.end()) {
		cout << "找到了:" << *p << endl;
	}
	else {
		cout << "没有找到0" << endl;
	}
	return 0;
}

二元谓词

案例:二元谓词 作为set容器排序规则

#include<iostream>
#include<string>
#include<set>

using namespace std;


void print_set(const set<int>& s) {
	for (set<int>::const_iterator p = s.begin(); p != s.end();p++) {
		cout << *p << " ";
	}
	cout << endl;
}

/*定义二元谓词函数 作为set容器的排序规则*/
class MyIntCompare {
public:
	bool operator()(int a, int b) const{
		return a > b;
	}
};


int main() {
	// 随机种子
	srand((unsigned int)time(NULL));
	// set 容器存储int默认 升序
	set<int>s1;
	for (int i = 0; i < 10; i++) {
		int n = (rand() % (50 - 10 + 1)) + 10;
		s1.insert(n);
	}
	print_set(s1); // 11 18 19 32 33 38 40 49

	// 构建set容器时,指定排序规则:降序
	set<int, MyIntCompare>s2;
	for (int i = 0; i < 10; i++) {
		int n = (rand() % (50 - 10 + 1)) + 10;
		s2.insert(n);
	}
	for (set<int>::const_iterator p = s2.begin(); p != s2.end(); p++) {
		cout << *p << " ";
	}
	cout << endl;// 42 36 20 18 17 15 14 13 10

	return 0;
}

内建函数对象

内建函数对象的意义

概念:

  • STL内建了一些函数对象

分类:

  • 算术仿函数

  • 关系仿函数

  • 逻辑仿函数

用法:

  • 这些仿函数所产生的对象,用法和一般函数完全相同
  • 使用内建函数对象,需要引入头文件 #include<functional>

算术仿函数

功能描述:

  • 实现四则运算
  • 其中negate是一元运算,其他都是二元运算

仿函数原型:

  • 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 negate<T> //取反仿函数
#include<iostream>
#include<string>
#include<functional>

using namespace std;

/*
算术仿函数:实现四则远算
	一元运算:negate 取反
	二元运算:
		plus 加法
		minus 减法
		multiplies 乘法
		divides 除法
		modulus 取模
*/
int main() {
	// plus 加法
	plus<int>p;
	cout << p(1,2)<< endl; // 3

	// minus 减法
	minus<int>mi;
	cout << mi(2, 1) << endl; // 1

	// multilies 乘法
	multiplies<int> mu;
	cout << mu(3, 7) << endl; // 21

	// 除法
	divides<int> di;
	cout << di(8,4) << endl; // 2

	// modules 取模
	modulus<int> mo;
	cout << mo(10, 7) << endl; // 3

	// negate 取反
	negate<int>ne;
	cout << ne(5) << endl; // -5

	return 0;
}

关系仿函数

功能描述:

  • 实现关系对比

仿函数原型:

  • 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_equal<T> //大于等于
  • template<class T> bool less<T> //小于
  • template<class T> bool less_equal<T> //小于等于
#include<iostream>
#include<string>
#include<functional>

using namespace std;

/*关系仿函数:
	equal_to 等于
	not_equal_to 不等于
	greater 大于
	greater_equal 大于等于
	less 小于
	less_equal 小于等于
*/
int main() {
	// 等于
	equal_to<int>et;
	cout << et(2,2) << endl; // true
	cout << et(1,2) << endl; // false

	// 不等于
	not_equal_to<int>net;
	cout << net(1,1) << endl; // false
	cout << net(2,1) << endl; // true

	// 大于
	greater<int>g;
	cout << g(1,2) << endl;// false
	cout << g(2,1) << endl;// true

	// 大于等于
	greater_equal<int>ge;
	cout << ge(2,2) << endl; // true

	// 小于
	less<int> le;
	cout << le(1, 2) << endl;// true

	// 小于等于
	less_equal<int>lee;
	cout << lee(2, 2) << endl; // true


	return 0;
}

逻辑仿函数

功能描述:

  • 实现逻辑运算

函数原型:

  • template<class T> bool logical_and<T> //逻辑与
  • template<class T> bool logical_or<T> //逻辑或
  • template<class T> bool logical_not<T> //逻辑非
#include<iostream>
#include<string>
#include<functional>

using namespace std;
/*
逻辑仿函数:
	logical_and 逻辑与
	logical_or 逻辑或
	logical_not 逻辑非
*/
int main() {
	// 逻辑与
	logical_and<bool>la;
	cout<< la(1 > 5, 6 > 5)<<endl; // false
	cout<< la(1 < 5, 6 > 5)<<endl; // true

	// 逻辑或
	logical_or<bool>lo;
	cout << lo(1 > 5, 6 > 5) << endl; // true
	cout << lo(1 < 5, 6 > 5) << endl; // true

	// 逻辑非
	logical_not<bool>ln;
	cout << ln(1 > 5) << endl; // true
	cout << ln(1 < 5) << endl; // false
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值