函数对象-C++

1.定义

2.特点

1.解释第一句

cpp 复制代码
#include<stdio.h>
using namespace std;
#include<string>
#include<map>
#include <iostream>
class print
{
public:
	void operator()(string s)
	{
		cout << s << endl;
	}
};
int main()
{
	print print;
	print("hello world");
	return 0;
}

2.解释第二句

可以有自己的状态:指的是他和普通的函数不同,例如他可以统计自己调用了几次。

也就是说,它可以向函数一样被调用,但是却拥有类的功能,他是类和函数的结合。

cpp 复制代码
#include<stdio.h>
using namespace std;
#include<string>
#include<map>
#include <iostream>
class print
{
public:
	print()
	{
		this->count = 0;
	}
	void operator()(string s)
	{
		cout << s << endl;
		this->count++;
	}
	int count;
};
int main()
{
	print print;
	print("hello world");
	print("hello world");
	print("hello world");
	print("hello world");
	print("hello world");
	cout << print.count << endl;
	return 0;
}

3.解释第三句

cpp 复制代码
#include<stdio.h>
using namespace std;
#include<string>
#include<map>
#include <iostream>
class print
{
public:
	print()
	{
		this->count = 0;
	}
	void operator()(string s)
	{
		cout << s << endl;
		this->count++;
	}
	int count;
};
void test(print p, string s)
{
	p(s);
}
void test01()
{
	print print;
	test(print, "C++");
}
int main()
{
	test01();
	return 0;
}

函数对象不仅可以作为函数参数,还可以再调用的函数中去使用自己重载过的小括号

具体见

void test(print p, string s)
{
p(s);
}

3.内建函数对象

1.定义

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

1.函数原型
2.取反仿函数(一元仿函数)
cpp 复制代码
#include<stdio.h>
using namespace std;
#include<string>
#include<vector>
#include<functional>
#include <iostream>
int main()
{
	negate<int>n;
	cout << n(10) << endl;
	return 0;
}
3.加法仿函数(二元仿函数)
cpp 复制代码
#include<stdio.h>
using namespace std;
#include<string>
#include<vector>
#include<functional>
#include <iostream>
int main()
{
	plus<int>n;
	cout << n(10,20) << endl;
	return 0;
}

注意:plus模板参数列表中只需要有一个int就行,是为了防止出现两个不同类型的数据相加

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

1.函数原型
2.代码
cpp 复制代码
#include<stdio.h>
using namespace std;
#include<string>
#include<vector>
#include<functional>
#include<algorithm>
#include <iostream>
int main()
{
	vector<int> v;
	v.push_back(1);
	v.push_back(5);
	v.push_back(3);
	v.push_back(2);
	sort(v.begin(), v.end(), greater<int>());
	for (int i = 0; i < v.size(); i++)
	{
		cout << v[i] << endl;
	}
	return 0;
}

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

1.函数原型
2.代码
cpp 复制代码
#include<stdio.h>
using namespace std;
#include<string>
#include<vector>
#include<functional>
#include<algorithm>
#include <iostream>
int main()
{
	vector<int> v;
	v.push_back(1);
	v.push_back(5);
	v.push_back(3);
	v.push_back(2);
	vector<int> v1;
	v1.resize(v.size());
	transform(v.begin(), v.end(),v1.begin(), logical_not<bool>());
	for (int i = 0; i < v.size(); i++)
	{
		cout << v[i] << " ";
	}
	cout << endl;
	for (int i = 0; i < v1.size(); i++)
	{
		cout << v1[i] << " ";
	}
	cout << endl;
	return 0;
}

其中用到了搬运函数transform,需要提前给他开辟相应的空间

v1.resize(v.size());
transform(v.begin(), v.end(),v1.begin(), logical_not<bool>());

4.注意

逻辑仿函数实际应用较少,了解即可

相关推荐
菜一头包11 分钟前
GNU,GDB,GCC,G++是什么?与其他编译器又有什么关系?
linux·c++·学习·gnu
虽千万人 吾往矣22 分钟前
golang context源码
android·开发语言·golang
天堂的恶魔94632 分钟前
C++项目 —— 基于多设计模式下的同步&异步日志系统(4)(双缓冲区异步任务处理器(AsyncLooper)设计)
开发语言·c++·设计模式
未来之窗软件服务1 小时前
数字人,磁盘不够No space left on device,修改python 执行环境-云GPU算力—未来之窗超算中心
linux·开发语言·python·数字人
爱的叹息1 小时前
【java实现+4种变体完整例子】排序算法中【桶排序】的详细解析,包含基础实现、常见变体的完整代码示例,以及各变体的对比表格
java·开发语言·排序算法
王齐家04061 小时前
每日一题算法——移除链表元素、反转链表
数据结构·算法·leetcode·链表
Zhuai-行淮1 小时前
施磊老师基于muduo网络库的集群聊天服务器(二)
开发语言·网络·c++
天天扭码1 小时前
一分钟解决 | 高频面试算法题——和为 K 的子数组(前缀和)
前端·算法·面试
ChoSeitaku1 小时前
NO.97十六届蓝桥杯备战|数论板块-最大公约数和最小公倍数|欧几里得算法|秦九韶算法|小红的gcd(C++)
c++·算法·蓝桥杯
椰萝Yerosius1 小时前
[图论]Prim
算法·图论