函数 仿函数
函数 仿函数
[TOC]
1. 仿函数
在C++中,仿函数(也称为函数对象)通过重载operator()的实例,使一个类的实例表现得像一个函数。[^1]
2. 仿函数的优势
-
状态保持:与普通函数相比,仿函数可以保持状态。你可以在类中定义成员变量来存储信息,并在
operator()调用中使用这些信息。 -
灵活性:由于它们是类的实例,仿函数可以拥有构造函数和析构函数,以及其他成员函数,提供比普通函数更多的灵活性。
-
可内联:在某些情况下,使用仿函数比普通函数指针更有效,因为编译器有可能对仿函数进行内联优化。
3. 仿函数的实现
(1) 一个简单的仿函数
假设我们想实现一个仿函数,用于计算两个数的和。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;
// 仿函数类
class Add {
public:
// 重载 () 运算符
int operator()(int a, int b) {
return a + b;
}
};
int main() {
Add add; // 创建仿函数的实例
cout << "The sum of 3 and 4 is " << add(3, 4) << endl; // 使用仿函数
return 0;
}
在这个例子中,Add类重载了operator(),使得Add的实例可以像函数一样被调用。你可以创建一个Add对象并像调用函数一样使用它来计算两个数的和。
输出:
1
The sum of 3 and 4 is 7
(2) 带状态的仿函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Counter {
int count;
public:
Counter() : count(0) {}
int operator()() {
return ++count;
}
};
int main() {
Counter counter;
cout << "First call: " << counter() << endl;
cout << "Second call: " << counter() << endl;
return 0;
}
这个例子中的Counter仿函数每次被调用时都会增加其计数。这展示了仿函数如何保持状态。每次调用counter(),计数增加,并且返回新的计数值。
输出:
1
2
First call: 1
Second call: 2
参考文章
本文由作者按照
CC BY 4.0
进行授权