黑马C++笔记——函数对象(仿函数)

函数对象

1.函数对象

1.概念

  • 重载函数调用的类,它的对象也成为函数对象
  • 函数对象使用重载的 () 时,行为类似于普通函数,所以被称为仿函数

2.本质

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

3. 函数对象的使用

  • 函数对象在使用时,可以向普通函数那样调用,可以有参数,也可以有返回值
  • 函数对象可以有自己的状态
  • 函数对象也可以作为一个参数传递

示例代码:

#include<iostream>
#include<string>
using namespace std;

class MyAdd {
public:
	int operator() (int a, int b) {
		return a + b;
	}
};

//1.函数对象在使用时,可以向普通函数那样,有参数,有返回值
void test01() {
	MyAdd myadd;
	cout << myadd(10, 20) << endl;
}

class MyPrint {
public:
	MyPrint() {
		this->count = 0;
	}

	void operator() (string s) {
		cout << s << endl;
		this->count++;//统计调用次数
	}
	int count;//内部的状态
};

//2.函数对象内部可以有自己的状态
void test02() {
	MyPrint myPrint;
	myPrint("Hello World");
	myPrint("Hello World");
	myPrint("Hello World");
	myPrint("Hello World");

	cout << "MyPrint的调用次数为:" << myPrint.count << endl;
}

//3.函数对象是可以作为一个参数进行传递的
void doPrint(MyPrint &mp,string s) {
	mp(s);
}
void test03() {
	MyPrint myPrint;
	doPrint(myPrint, "aaaaaaa");
}

int main() {
	//test01();
	//test02();
	test03();
	system("pause");
	return 0;
}

2.谓词

1.概念

  • 返回bool类型的仿函数被称为谓词
  • operator() 有一个参数就是一元谓词
  • operator() 有两个个参数就是二元谓词

一元谓词

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;

class GreaterFive {
public:
	//一元谓词
	bool operator() (int val) {
		return val > 5;
	}
};

void test() {
	vector<int> v{ 1,2,3,4,5,6,7,8,9 };
	//查找大于5的元素
	auto it = find_if(v.begin(), v.end(), GreaterFive());
	if (it == v.end()) {
		cout << "未找到大于5的元素" << endl;
	}
	else {
		cout << "找到了大于5的元素:" << *it << endl;
	}
}

int main() {
	test();
	system("pause");
	return 0;
}

二元谓词

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;

class MyCmp {
public:
	bool operator() (int a, int b) {
		return a > b;
	}
};

void test() {
	vector<int> v{ 5,4,6,9,8,12,54,33,62,1,2,3 };

	//升序排序
	sort(v.begin(), v.end());
	for (auto it = v.begin(); it != v.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
	cout << "-------------------------------------------------------" << endl;

	//利用二元谓词降序排序
	sort(v.begin(), v.end(), MyCmp());
	for (auto it = v.begin(); it != v.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

int main() {
	test();
	system("pause");
	return 0;
}

2.内建函数对象

分类

  • 算数仿函数
  • 关系仿函数
  • 逻辑仿函数

算数仿函数

#include<iostream>
#include<functional>
using namespace std;

//算数仿函数包括 加减乘除四则运算 这里只演示取相反数
//一元仿函数 negate 取相反数
void test01() {
	negate<int> n;
	cout << "10 的相反数为 " << n(10) << endl;
}

//二元仿函数 plus 两者相加
void test02() {
	plus<int> p;
	cout << "10 + 20 = " << p(10, 20) << endl;
}

int main() {
	test01();
	test02();
	system("pause");
	return 0;
}

关系仿函数

#include<iostream>
#include<algorithm>
#include<vector>
#include<functional>
using namespace std;

//关系仿函数包括大于 大于等于 小于 小于等于
//测试大于关系仿函数 greater()
void test() {
	vector<int> v{ 2,5,6,74,12,3,65,45,12,3,6,9,8,10 };
	//遍历输出
	for (auto it = v.begin(); it != v.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;

	//降序排序
	sort(v.begin(), v.end(), greater<int>());

	for (auto it = v.begin(); it != v.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;

}

int main() {
	test();
	system("pause");
	return 0;
}

逻辑仿函数

#include<iostream>
#include<algorithm>
#include<functional>
#include<vector>
using namespace std;

//逻辑仿函数有 逻辑与 逻辑或 逻辑非
//测试逻辑仿函数 逻辑非 logical_not<bool>() 
void test() {
	vector<bool> v{ false,true,false,true };
	vector<bool> temp;
	temp.resize(v.size());

	for (auto it = v.begin(); it != v.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;

	transform(v.begin(), v.end(), temp.begin(), logical_not<bool>());

	for (auto it = temp.begin(); it != temp.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

int main() {
	test();
	system("pause");
	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值