C++STL函数对象

tech2026-06-16  3

STL- 函数对象

函数对象

函数对象概念

概念:

重载函数调用操作符的类,其对象常称为函数对象函数对象使用重载的()时,行为类似函数调用,也叫仿函数

本质:

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

函数对象使用

特点:

函数对象在使用时,可以像普通函数那样调用, 可以有参数,可以有返回值函数对象超出普通函数的概念,函数对象可以有自己的状态函数对象可以作为参数传递 #include <iostream> #include <string> using namespace std; class MyAdd { public: int operator()(int v1, int v2) { return v1 + v2; } }; void test01() { MyAdd myAdd; cout << myAdd(10, 10) << endl; } //2.函数对象可以有自己的状态 class MyPrint { public: MyPrint() { count = 0; } void operator()(string test) { cout << test << endl; count++; //统计使用次数 } int count = 0; //内部自己的状态 }; void test02() { MyPrint myprint; myprint("Hello World"); myprint("Hello World"); myprint("Hello World"); cout << "myPrint调用次数: " << myprint.count << endl; } //3.函数对象可以作为参数传递 void doPrint(MyPrint& mp, string test) { mp(test); } void test03() { MyPrint myPrint; doPrint(myPrint, "Hello C++"); } int main() { cout << "test01()" << endl; test01(); cout << "test02()" << endl; test02(); cout << "test03()" << endl; test03(); return 0; } //仿函数写法非常灵活, 可以作为参数进行传递
最新回复(0)