C++从入门到精通 第十五章(STL函数对象)

2024-02-22 07:20

本文主要是介绍C++从入门到精通 第十五章(STL函数对象),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 写在前面:

  1. 本系列专栏主要介绍C++的相关知识,思路以下面的参考链接教程为主,大部分笔记也出自该教程,笔者的原创部分主要在示例代码的注释部分。
  2. 除了参考下面的链接教程以外,笔者还参考了其它的一些C++教材(比如计算机二级教材和C语言教材),笔者认为重要的部分大多都会用粗体标注(未被标注出的部分可能全是重点,可根据相关部分的示例代码量和注释量判断,或者根据实际经验判断)。
  3. 如有错漏欢迎指出。

参考教程:黑马程序员匠心之作|C++教程从0到1入门编程,学习编程不再难_哔哩哔哩_bilibili

一、函数对象

1、函数对象概念

(1)重载函数调用操作符的类,其对象常称为函数对象

(2)函数对象使用重载的()时,行为类似函数调用,也叫仿函数

(3)函数对象(仿函数)的本质是一个,不是一个函数

2、函数对象的使用

(1)函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值。

(2)函数对象超出普通函数的概念,函数对象可以有自己的状态。

(3)函数对象可以作为参数传递。

#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;
}class MyPrint
{
public:MyPrint(){count = 0;}void operator()(string test){cout << test << endl;count++;}int count;    //记录调用次数
};
void test02()
{MyPrint myPrint;myPrint("Hello world");myPrint("Hello world");myPrint("Hello world");cout << myPrint.count << endl;
}void doPrint(MyPrint & mp, string test)
{mp(test);
}
void test03()
{MyPrint myPrint;doPrint(myPrint, "Hello");
}int main() {test01();test02();test03();system("pause");return 0;
}

二、谓词

1、谓词概念

(1)返回bool类型的仿函数称为谓词。

(2)如果operator()接受一个参数,那么就叫做一元谓词;如果operator()接受两个参数,那么就叫做二元谓词。

2、一元谓词

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;class GreaterFive
{
public:bool operator()(int val)   //一个参数{return val > 5;}
};void test01()
{vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}vector<int>::iterator it;it = find_if(v.begin(), v.end(), GreaterFive());   //第三个是匿名函数对象if (it == v.end()){cout << "未找到" << endl;}else{cout << "找到了大于5的数字为:" << *it << endl;}
}int main() {test01();system("pause");return 0;
}

3、二元谓词

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;class MyCompare
{
public:bool operator()(int va1,int va2)   //两个参数{return va1 > va2;}
};void test01()
{vector<int>v;v.push_back(10);v.push_back(40);v.push_back(20);v.push_back(50);v.push_back(30);vector<int>::iterator it = v.begin();for (; it != v.end(); it++){cout << *it << " ";}cout << endl;vector<int>::iterator it2 = v.begin();sort(v.begin(), v.end(), MyCompare());  //改变算法策略,变为从大到小排序(默认从小到大)for (; it2 != v.end(); it2++){cout << *it2 << " ";}cout << endl;
}int main() {test01();system("pause");return 0;
}

三、内建函数对象

1、内建函数对象意义

(1)STL内建了一些函数对象,分为算术仿函数、关系仿函数、逻辑仿函数三类,这些仿函数所产生的对象,用法和一般函数完全相同。

(2)使用内建函数对象,需要引入头文件<functional>。

2、算术仿函数

(1)功能描述:实现四则运算,其中negate是一元运算,其它都是二元运算。

(2)仿函数原型:

template<class T> T plus<T>       //加法仿函数

template<class T> T minus<T>     //减法仿函数

template<class T> T multiplies<T>  //乘法仿函数

template<class T> T divides<T>    //除法仿函数

template<class T> T modulus<T>   //取模仿函数

template<class T> T negate<T>    //取反仿函数

#include<iostream>
#include<functional>
using namespace std;void test01()
{negate<int>n;cout << n(50) << endl;plus<int>p;cout << p(10, 20) << endl;
}int main() {test01();system("pause");return 0;
}

3、关系仿函数

(1)功能描述:实现关系对比。

(2)仿函数原型:

template<class T> bool equal_to<T>       //等于

template<class T> bool not_equal_to<T>   //不等于

template<class T> bool greater<T>        //大于

template<class T> bool greater_equal<T>  //大于等于

template<class T> bool less<T>          //小于

template<class T> bool less_equal<T>     //小于等于

#include<iostream>
#include<vector>
#include<functional>
#include<algorithm>
using namespace std;void test01()
{vector<int>v;v.push_back(10);v.push_back(30);v.push_back(40);v.push_back(20);v.push_back(50);sort(v.begin(), v.end(), greater<int>());   //改为降序for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {cout << *it << " ";}cout << endl;
}int main() {test01();system("pause");return 0;
}

4、逻辑仿函数

(1)功能描述:实现逻辑运算。

(2)函数原型:

template<class T> bool logical_and<T>   //逻辑与

template<class T> bool logical_or<T>    //逻辑或

template<class T> bool logical_not<T>   //逻辑非

#include<iostream>
#include<vector>
#include<functional>
#include<algorithm>
using namespace std;void test01()
{vector<bool>v;v.push_back(true);v.push_back(true);v.push_back(false);v.push_back(true);v.push_back(false);for (vector<bool>::iterator it = v.begin(); it != v.end(); it++) {cout << *it << " ";}cout << endl;//将容器v搬运到容器v2中,利用逻辑非执行取反操作vector<bool>v2;v2.resize(v.size());transform(v.begin(), v.end(), v2.begin(), logical_not<bool>());for (vector<bool>::iterator it2 = v2.begin(); it2 != v2.end(); it2++){cout << *it2 << " ";}cout << endl;
}int main() {test01();system("pause");return 0;
}

这篇关于C++从入门到精通 第十五章(STL函数对象)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/734514

相关文章

利用c++判断水仙花数并输出示例代码

《利用c++判断水仙花数并输出示例代码》水仙花数是指一个三位数,其各位数字的立方和恰好等于该数本身,:本文主要介绍利用c++判断水仙花数并输出的相关资料,文中通过代码介绍的非常详细,需要的朋友可以... 以下是使用C++实现的相同逻辑代码:#include <IOStream>#include <vec

基于C++的UDP网络通信系统设计与实现详解

《基于C++的UDP网络通信系统设计与实现详解》在网络编程领域,UDP作为一种无连接的传输层协议,以其高效、低延迟的特性在实时性要求高的应用场景中占据重要地位,下面我们就来看看如何从零开始构建一个完整... 目录前言一、UDP服务器UdpServer.hpp1.1 基本框架设计1.2 初始化函数Init详解

Mybatis对MySQL if 函数的不支持问题解读

《Mybatis对MySQLif函数的不支持问题解读》接手项目后,为了实现多租户功能,引入了Mybatis-plus,发现之前运行正常的SQL语句报错,原因是Mybatis不支持MySQL的if函... 目录MyBATis对mysql if 函数的不支持问题描述经过查询网上搜索资料找到原因解决方案总结Myb

C++ 右值引用(rvalue references)与移动语义(move semantics)深度解析

《C++右值引用(rvaluereferences)与移动语义(movesemantics)深度解析》文章主要介绍了C++右值引用和移动语义的设计动机、基本概念、实现方式以及在实际编程中的应用,... 目录一、右值引用(rvalue references)与移动语义(move semantics)设计动机1

Python容器转换与共有函数举例详解

《Python容器转换与共有函数举例详解》Python容器是Python编程语言中非常基础且重要的概念,它们提供了数据的存储和组织方式,下面:本文主要介绍Python容器转换与共有函数的相关资料,... 目录python容器转换与共有函数详解一、容器类型概览二、容器类型转换1. 基本容器转换2. 高级转换示

C++ move 的作用详解及陷阱最佳实践

《C++move的作用详解及陷阱最佳实践》文章详细介绍了C++中的`std::move`函数的作用,包括为什么需要它、它的本质、典型使用场景、以及一些常见陷阱和最佳实践,感兴趣的朋友跟随小编一起看... 目录C++ move 的作用详解一、一句话总结二、为什么需要 move?C++98/03 的痛点⚡C++

详解C++ 存储二进制数据容器的几种方法

《详解C++存储二进制数据容器的几种方法》本文主要介绍了详解C++存储二进制数据容器,包括std::vector、std::array、std::string、std::bitset和std::ve... 目录1.std::vector<uint8_t>(最常用)特点:适用场景:示例:2.std::arra

C++构造函数中explicit详解

《C++构造函数中explicit详解》explicit关键字用于修饰单参数构造函数或可以看作单参数的构造函数,阻止编译器进行隐式类型转换或拷贝初始化,本文就来介绍explicit的使用,感兴趣的可以... 目录1. 什么是explicit2. 隐式转换的问题3.explicit的使用示例基本用法多参数构造

C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解

《C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解》:本文主要介绍C++,C#,Rust,Go,Java,Python,JavaScript性能对比全面... 目录编程语言性能对比、核心优势与最佳使用场景性能对比表格C++C#RustGoJavapythonjav

C++打印 vector的几种方法小结

《C++打印vector的几种方法小结》本文介绍了C++中遍历vector的几种方法,包括使用迭代器、auto关键字、typedef、计数器以及C++11引入的范围基础循环,具有一定的参考价值,感兴... 目录1. 使用迭代器2. 使用 auto (C++11) / typedef / type alias