【STL十五】函数对象:可调用对象
- 一、可调用对象
-
- 二、普通函数
-
- 三、仿函数——函数对象
- 四、lambda表达式
- 五、类的静态成员函数——(作为可调用对象,没有实用价值)
- 六、类的非静态成员函数——(作为可调用对象,没有实用价值)
- 七、可被转换为函数的类的对象——(作为可调用对象,没有实用价值)
- 简介
1、可调用对象这个概念在哔哩哔哩的一个视频中看到的概念。
2、可调用对象:平时大家会用到的就三个:普通函数、仿函数、lambda表达式。
3、本篇文章的重点在函数指针、函数引用;其它另外三种没有实用价值。
一、可调用对象
1、定义
- 可调用对象:可以像函数一样调用的对象。
- 可调用对象有类型,可以用指针存储它们的地址,可以被引用(类的成员函数除外)
2、分类
- 普通函数
- 仿函数
- lambda函数
- 类的静态成员函数——(作为可调用对象,没有实用价值)
- 类的非静态成员函数——(作为可调用对象,没有实用价值)
- 可被转换为函数的类的对象——(作为可调用对象,没有实用价值)
二、普通函数
1、直接调用普通函数
#include <iostream>
#include<functional>
using namespace std;
using Fun = void(int, const string&);
Fun show;
void show(int i, const string& message)
{
cout << "第" << i << "个: " << message;
}
int main()
{
show(1, "可能南方的阳光 照着北方的风");
}
输出
第1个: 可能南方的阳光 照着北方的风
2、函数指针、函数引用
using Fun = void(int, const string&);
Fun* fp1 = show;
- 用别名可以定义引用,
我们很少使用函数引用,因为引用的本质是指针,所以他们是一样的。
`Fun& fr1 = show;
#include <iostream>
#include<functional>
using namespace std;
using Fun = void(int, const string&);
Fun show;
void show(int i, const string& message)
{
cout << "第" << i << "个: " << message << endl;
}
int main()
{
Fun* fp1 = show;
Fun& fr1 = show;
fp1(2, "可能西安城墙上 有人誓言不分");
fr1(3, "可能西安城墙上 有人誓言不分");
void(*fp2)(int, const string&) = show;
void(&fr2)(int, const string&) = show;
fp2(4, "可能要去到大理 才算爱得认真");
fr2(5, "可能要去到大理 才算爱得认真");
}
输出
第2个: 可能西安城墙上 有人誓言不分
第3个: 可能西安城墙上 有人誓言不分
第4个: 可能要去到大理 才算爱得认真
第5个: 可能要去到大理 才算爱得认真
三、仿函数——函数对象
- 参考:【STL十四】函数对象(function object)_仿函数(functor)——lambda表达式
#include <iostream>
using namespace std;
class Print
{
public:
void operator()(const char str[])
{
cout << str << endl;
}
};
int main() {
Print ob;
ob("hello world");
Print()("hello world");
Print& obr = ob;
obr("hello world");
}
输出
hello world
hello world
hello world
四、lambda表达式
- 参考:【STL十四】函数对象(function object)_仿函数(functor)——lambda表达式
五、类的静态成员函数——(作为可调用对象,没有实用价值)
#include <iostream>
using namespace std;
using Fun = void(int, const string&);
struct AA
{
static void show(int bh, const string& message) {
cout << "第" << bh << "个," << message << endl;
}
};
int main()
{
AA::show(6, " 可能(程响)");
Fun* fp1 = AA::show;
Fun& fr1 = AA::show;
fp1(7, " 可能北京的后海 许多漂泊的魂");
fr1(8, " 可能北京的后海 许多漂泊的魂");
void(*fp2)(int, const string&) = AA::show;
void(&fr2)(int, const string&) = AA::show;
fp2(9, " 可能成都小酒馆 有群孤独的人");
fr2(10, "可能北京的后海 许多漂泊的魂");
}
输出
第6个, 可能(程响)
第7个, 可能北京的后海 许多漂泊的魂
第8个, 可能北京的后海 许多漂泊的魂
第9个, 可能成都小酒馆 有群孤独的人
第10个,可能北京的后海 许多漂泊的魂
六、类的非静态成员函数——(作为可调用对象,没有实用价值)
类的非静态成员函数= 类的普通成员函数
void( *fp2)(int, const string&) = show;
void (CC:: * fp1)(int, const string&) = &CC::show;
= show
= show
= &CC::show
#include <iostream>
using namespace std;
class CC
{
public:
void show(int bh, const string& message) {
cout << "第" << bh << "行 ," << message << endl;
}
};
int main()
{
CC cc;
cc.show(14, "可能(程响)");
using pFun = void (CC::*)(int, const string&);
pFun fp2 = &CC::show;
(cc.*fp2)(15, "可能拥有过梦想 才能叫做青春");
void (CC:: * fp1)(int, const string&) = &CC::show;
(cc.*fp1)(16, "可能拥有过梦想 才能叫做青春");
}
输出
第14行 ,可能(程响)
第15行 ,可能拥有过梦想 才能叫做青春
第16行 ,可能拥有过梦想 才能叫做青春
七、可被转换为函数的类的对象——(作为可调用对象,没有实用价值)
参考
1、C++ STL 容器库 中文文档
2、STL教程:C++ STL快速入门
3、https://www.apiref.com/cpp-zh/cpp/header.html
4、https://en.cppreference.com/w/cpp/container
5、WIKI教程_C ++标准库_C++ Library - <iterator>
6、哔哩哔哩_系统化学习C++_C++11神器之可调用对象包装器和绑定器
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)