在完成对C语言的学习后,我最近开始了对C++和Java的学习,目前跟着视频学习了一些语法,也跟着敲了一些代码,有了一定的掌握程度。现在将跟着视频做的笔记进行整理。本篇博客是整理C++知识点的第二十九篇博客。
本篇博客介绍了C++的函数对象。
本系列博客所有C++代码都在Visual Studio 2022环境下编译运行。程序为64位。
目录
重载函数调用操作符的类,其对象常称为函数对象。函数对象使用重载的()时,行为类似于函数调用,也称为仿函数。函数对象是一个类,不是一个函数。
函数对象在使用时,可以像普通函数那样调用,可以有参数,也可以有返回值。
函数对象可以有自己的状态,也可以作为参数传递。
- #include
- using namespace std;
- class test
- {
- public:
- int operator()(int a, int b) {
- return a + b;
- }
- };
- int main(void)
- {
- test t;
- cout << t(10, 15) << endl;
- return 0;
- }
程序的输出是:
25
- #include
- #include
- using namespace std;
- class test
- {
- public:
- int time;
- test() {
- time = 0;
- }
- void operator() (string str) {
- cout << str << endl;
- time += 1;
- }
- };
-
- int main(void)
- {
- test t;
- t("Hello C++");
- t("Hello C++");
- t("Hello C++");
- t("Hello C++");
- t("Hello C++");
- cout << t.time << endl;
- return 0;
- }
test类有成员变量time,构造函数将time的值定为0。重载()接受一个string参数,将其输出,同时time加一。main函数创建了一个test类对象t,调用()五次,并输出随后time的值。
程序的输出是:
Hello C++
Hello C++
Hello C++
Hello C++
Hello C++
5
time统计了调用()的次数。
- #include
- #include
- using namespace std;
- class test
- {
- public:
- void operator() (string s) {
- cout << s << endl;
- }
- };
-
- void dotest(test& t)
- {
- t("Hello C++");
- }
- int main(void)
- {
- test t;
- dotest(t);
- return 0;
- }
test类重载了()。main函数创建了一个test类对象t,并将其作为参数传递给dotest函数。dotest函数调用参数的()。
程序的输出是:
Hello C++
返回bool类型的仿函数称为谓词。如果接受一个参数,就叫做一元谓词,如果接受两个参数,就叫二元谓词。
- #include
- #include
- #include
- using namespace std;
- class test
- {
- public:
- bool operator()(int num) {
- return num > 5;
- }
- };
-
- int main(void)
- {
- vector<int> v;
- int i;
- for (i = 1; i <= 10; i += 1) {
- v.push_back(i);
- }
-
- vector<int>::iterator it = find_if(v.begin(), v.end(), test());
- if (it == v.end()) {
- cout << "Not found" << endl;
- }
- else {
- cout << "Have found " << *it << endl;
- }
- return 0;
- }
test函数重载了(),返回参数是否大于5,大于为true,否则为false。main函数创建了一个vector容器,加入1至10。随后使用了find_if算法,该算法在此处返回大于5的第一个元素的迭代器(没有就是.end,但是本例很显然是有的,这个算法后面详细介绍)。随后判断是否找到,找到还会输出其指向元素。
程序的输出是:
Have found 6
- #include
- #include
- #include
- using namespace std;
- class test
- {
- public:
- bool operator()(int a, int b) {
- return a > b;
- }
- };
-
- int main(void)
- {
- vector<int> v;
- v.push_back(20);
- v.push_back(40);
- v.push_back(30);
- v.push_back(50);
- v.push_back(10);
- for (vector<int>::iterator it = v.begin(); it != v.end(); it += 1) {
- cout << *it << " ";
- }
- cout << endl;
-
- sort(v.begin(), v.end(), test());
- for (vector<int>::iterator it = v.begin(); it != v.end(); it += 1) {
- cout << *it << " ";
- }
- cout << endl;
- return 0;
- }
test类重载了(),判断两个参数的大小关系。vector容器加入了五个int类型常量,随后使用了sort进行排序,排序时第三个参数就是test(),因此会降序排序。
程序的输出是:
20 40 30 50 10
50 40 30 20 10
STL内建了一些函数对象,分为算术仿函数,关系仿函数和逻辑仿函数。这些仿函数产生的对象,用法和一般函数完全相同。使用这些函数对象需要包含头文件functional。
算术仿函数实现四则运算,其中negate是一元运算,其他都是二元运算。
template
template
template
template
template
template
- #include
- #include
- using namespace std;
- int main(void)
- {
- plus<int> p;
- cout << p(10, 15) << endl;
- minus<int> m1;
- cout << m1(20, 12) << endl;
- multiplies<int> m2;
- cout << m2(6, 8) << endl;
- divides<int> d;
- cout << d(20, 7) << endl;
-
- modulus<int> m3;
- cout << m3(20,7) << endl;
- negate<int> n;
- cout << n(10) << endl;
- return 0;
- }
程序的输出是:
25
8
48
2
6
-10
关系仿函数用于实现关系对比。
template
template
template
template
template
template
- #include
- #include
- #include
- #include
- using namespace std;
- int main(void)
- {
- vector<int> v;
- v.push_back(20);
- v.push_back(30);
- v.push_back(40);
- v.push_back(10);
- v.push_back(50);
- sort(v.begin(), v.end(), greater<int>());
- for (vector<int>::iterator it = v.begin(); it < v.end(); it += 1) {
- cout << *it << " ";
- }
- cout << endl;
- return 0;
- }
sort的第三个参数是greater
程序的输出是:
50 40 30 20 10
- #include
- #include
- using namespace std;
- int main(void)
- {
- equal_to<int> et;
- cout << et(10, 20) << endl;
- cout << et(10, 10) << endl;
- not_equal_to<int> net;
- cout << net(10, 20) << endl;
- cout << net(10, 10) << endl;
-
- greater<int> g;
- cout << g(10, 0) << endl;
- cout << g(10, 10) << endl;
- cout << g(10, 15) << endl;
- greater_equal<int> ge;
- cout << ge(10, 0) << endl;
- cout << ge(10, 10) << endl;
- cout << ge(10, 15) << endl;
-
- less<int> l;
- cout << l(10, 0) << endl;
- cout << l(10, 10) << endl;
- cout << l(10, 15) << endl;
-
- less_equal<int> le;
- cout << le(10, 0) << endl;
- cout << le(10, 10) << endl;
- cout << le(10, 15) << endl;
- return 0;
- }
程序的输出是:
0
1
1
0
1
0
0
1
1
0
0
0
1
0
1
1
逻辑仿函数用于实现逻辑运算。
template
template
template
- #include
- #include
- #include
- #include
- using namespace std;
- int main(void)
- {
- vector<bool> v1;
- v1.push_back(true);
- v1.push_back(false);
- v1.push_back(true);
- v1.push_back(false);
-
- for (vector<bool>::iterator it = v1.begin(); it != v1.end(); ++it) {
- cout << *it << " ";
- }
- cout << endl;
-
- vector<bool> v2;
- v2.resize(v1.size());
- transform(v1.begin(), v1.end(), v2.begin(), logical_not<bool>());
- for (vector<bool>::iterator it = v2.begin(); it != v2.end(); ++it) {
- cout << *it << " ";
- }
- cout << endl;
- return 0;
- }
transform将v1的每个元素进行逻辑反运算后赋给v2,此算法后面介绍。
程序的输出是:
1 0 1 0
0 1 0 1