【CPP】仿函数

概述: C++ 防函数使用和说明

[toc]

仿函数

顾名思义,就是"仿造函数"的意思,它并不是函数,但是却有着类似于函数的行为.

我们在编程的时候,有时候会发现这样一种情况:有一些相同的功能,会在不同的成员函数中出现. 这时候按照一般的技巧,我们会将这些相同的功能独立出来进行封装. 但是写一个公共函数的话,函数用到的一些变量就可能被迫成为公共的全局变量. 而且,仅仅为了复用这一片代码,就要单独写一个函数,也不是很好维护,这个时候就可以用仿函数了.

按照百度词条的说法:写一个简单的类,除了那些维护一个类的成员函数外,就只是实现一个operator(),在类实例化的时候,就将要用的,非参数的元素传入类中. 这样写的好处是:

  1. 免去了一些公共变量的全局化的维护.
  2. 可以使那些代码独立出来,以便下次复用.
  3. 这些仿函数还可以用关联,聚合,依赖的类之间的关系,与用到他们的类组合在一起,这样有利于资源的管理.

作用:

  1. 可替代函数指针,使用更加灵活
  2. 可存储状态,形成一种类似于闭包的机制

STL 提供的仿函数

graph LR
A(STL提供的仿函数) --> B(算术类仿函数)
A --> C(关系运算符仿函数)
A --> D(逻辑运算仿函数)
A --> E("位运算函数(since C++11)")

B --> B1(plus<T>) --> 相加
B --> B2(minus<T>) --> 相减
B --> B3(multiplies<T>) --> 相乘
B --> B4(divides<T>) --> 相除
B --> B5(modulus<T>) --> 取模
B --> B6(negate<T>) --> 取否

C --> C1(equal_to<T>) --> 等于
C --> C2(not_equal_to<T>) --> 不等于
C --> C3(greater<T>) --> 大于
C --> C4(greater_equal<T>) --> 大于等于
C --> C5(less<T>) --> 小于
C --> C6(less_equal<T>) --> 小于等于

D --> D1(logical_and<T>) --> 逻辑与
D --> D2(logical_or<T>) --> 逻辑或
D --> D3(logical_no<T>) --> 逻辑非

E --> E1(bit_and<T>) --> 按位与
E --> E2(bit_or<T>) --> 按位或
E --> E3(bit_xor<T>) --> 按位异或

STL 中 less<T> 的用法示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/* 
/home/muten003/CPP/functor/002.cpp
*/
#include <iostream>
#include <algorithm>
using namespace std;

class MyClass
{
public:
int x;
int y;
friend bool operator<(MyClass a, MyClass b);
};

class Compare
{
friend bool operator<(MyClass a, MyClass b)
{
return a.x < b.x;
}
};

int main()
{
less<MyClass> myclassLes;
MyClass myclass[3];
myclass[0].x = 2;
myclass[1].x = 1;
myclass[2].x = 3;
sort(myclass, myclass + 2, myclassLes);

for_each(sortClass, sortClass + 3, [] (UseLessClass obj) {
cout << obj.y << ":" << obj.x << endl;
});
}

输出:

1
1 2 3

使用仿函数的优点

将一个个功能通过类来实现,大多数情况下会加大代码量. 仿函数也是如此,但是仿函数却有着许多优点,
除了上述的有利于资源管理、有利于代码维护外,还具有这些特点

  1. 智能性:仿函数是通过仿函数类来实现的,既然是类,里面就可以储存许多变量还有其他的一些信息.我们可以利用这个特征实现许多纯的函数实现不了的功能,举个简单的例子:我若再仿函数中加入了统计调用函数次数的功能。那么在我使用仿函数的时候,就可以自动实现统计次数的功能. 所以说,更加智能。
  2. 灵活性:或者说,每个仿函数都可以有自己的类型. 由于仿函数是泛型编程的一个例证, 它可以任意 指定类型。
  3. 通常比一般函数速度更快:尤其是大量调用函数的过程中. 比如说:STL排序的时候,所需要的那个排序规则(为一个bool型的函数).

使用实例

简单示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
using namespace std;
class Compare
{
public:
bool operator()(int a, int b)
{
return a < b;
}
};
template <class Function>
void comp(int a, int b, Function func)
{
if (func(a, b))
{
cout << b << " is bigger than " << a << endl;
}
else
{
cout << a << " is bigger than " << b << endl;
}
}
int main()
{
int a = 6;
int b = 2;
Compare com;
comp(a, b, com);
}

与普通函数对比

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <set>
#include <algorithm>
using namespace std;

class CompareObject {
public:
void operator()() {
cout << "仿函数" << endl;
}

// 重载
void operator()(int number, int number2) {
cout << "仿函数" << endl;
}
};

// 查看c++ for_each源码自定义
class ShowActionObj {
public:
void operator()(int content) {
cout << "custom 仿函数" << content << endl;
}
};

// 回调方式
void showAction(int content) {
cout << "custom 普通函数" << content << endl;

}

int main()
{
// 直接调用仿函数
CompareObject fun1;
fun1();

// 以回调方式调用
set<int> setVar{ 10,20,30,40,50,60 };

cout << "使用仿函数:" << endl;
for_each(setVar.begin(), setVar.end(), ShowActionObj());

cout << "使用普通函数" << endl;
for_each(setVar.begin(), setVar.end(), showAction);

return 0;
}

输出如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
仿函数
使用仿函数:
custom 仿函数10
custom 仿函数20
custom 仿函数30
custom 仿函数40
custom 仿函数50
custom 仿函数60
使用普通函数
custom 普通函数10
custom 普通函数20
custom 普通函数30
custom 普通函数40
custom 普通函数50
custom 普通函数60

补充 for_eace 的源码:

1
2
3
4
5
6
7
8
9
10
11
template <class _InIt, class _Fn>
_CONSTEXPR20 _Fn for_each(_InIt _First, _InIt _Last, _Fn _Func) { // perform function for each element [_First, _Last)
_Adl_verify_range(_First, _Last);
auto _UFirst = _Get_unwrapped(_First);
const auto _ULast = _Get_unwrapped(_Last);
for (; _UFirst != _ULast; ++_UFirst) {
_Func(*_UFirst);
}

return _Func;
}

参考文章


【CPP】仿函数
https://hodlyounger.github.io/B_Code/CPP/【CPP】仿函数/
作者
mingming
发布于
2023年10月27日
许可协议