当前位置: 首页 > news >正文

《21天学通C++》(第二十一章)理解函数对象

什么是函数对象?
函数对象是一种特殊类型的类,它重载了函数调用操作符 operator(),使得类的实例可以像函数一样被调用。
什么是谓词?
谓词是指一个能够返回布尔值(true或false)的函数或函数对象

1.一元函数

一元函数是指接受单个参数的函数

#include <iostream>
#include <vector>
#include <algorithm>// 定义一元函数对象,用于打印整数
struct Print {//定义一个结构体void operator()(int x) const {//重载operator()std::cout << x << " ";}
};int main() {std::vector<int> numbers = {1, 2, 3, 4, 5};// 使用一元函数对象显示集合内容std::for_each(numbers.begin(), numbers.end(), Print());//开始迭代器,结束迭代器,操作函数(函数对象或Lambda表达式)//调用Print()std::cout << std::endl;system("pause");return 0;
}

2.一元谓词

一元谓词是一种特殊的一元函数,它接受单个参数并返回一个布尔值

#include <iostream>
#include <vector>
#include <algorithm>// 定义一个一元谓词,用于判断是否为某个数字的整数
struct IsMultipleOf {//int Divisor;IsMultipleOf(int Divisor) : Divisor(Divisor) {}bool operator()(int number) const {//重载operator(),返回布尔值return ((number % Divisor) == 0);}
};int main() {std::vector<int> numbers = {1, 2, 3, 4, 10};// 使用一元谓词 IsMultipleOf 来查找第一个是5的倍数的数字auto it = std::find_if(numbers.cbegin(), numbers.cend(), IsMultipleOf(5));if (it != numbers.cend()) {std::cout << "The  number is: " << *it << std::endl;} else {std::cout << "No number" << std::endl;}system("pause");return 0;
}

3.二元函数

一元函数是指接受两个参数的函数

#include <iostream>
#include <vector>
#include <algorithm>// 定义一个二元函数,实现两个数相乘
struct AddFunctor {//定义一个结构体int operator()(int a, int b) const {//重载operator()return a * b;}
};int main() {std::vector<int> numbers1 = {1, 2, 3, 4, 5};std::vector<int> numbers2 = {5, 4, 4, 2, 2};// 使用二元函数计算两个向量对应元素的乘积std::vector<int> result(numbers1.size());//定义一个新的vector用来存储结果std::transform(numbers1.begin(), numbers1.end(), numbers2.begin(), result.begin(), AddFunctor());//调用AddFunctor()// 显示结果for (int num : result) {std::cout << num << " ";}std::cout << std::endl;system("pause");return 0;
}

4.二元谓词

一元谓词是一种特殊的二元函数,它接受两个参数并返回一个布尔值

#include <iostream>
#include <vector>
#include <algorithm>// 二元谓词,用于比较两个整数的大小
struct GreaterThan {bool operator()(int a, int b) const {return a > b;  // 返回 a 是否大于 b}
};int main() {std::vector<int> numbers = {10, 20, 30, 40, 50};// 使用二元谓词对向量进行降序排序std::sort(numbers.begin(), numbers.end(), GreaterThan());// 输出排序后的向量for (int num : numbers) {std::cout << num << " ";}std::cout << std::endl;system("pause");return 0;
}
http://www.lryc.cn/news/345928.html

相关文章:

  • 2024.1.1 IntelliJ IDEA 使用记录
  • 扩展van Emde Boas树以支持卫星数据:设计与实现
  • 玩游戏专用远程控制软件
  • 机器人规划控制——工程化——心得日记-20240510
  • 2024年成都市标杆场景项目申报条件对象、奖励和认定材料流程
  • 前端Vue uView 组件<u-search> 自定义右侧搜索按钮样式
  • 【Linux网络编程】I/O多路转接之select
  • 三下乡社会实践投稿攻略在这里
  • 银河麒麟桌面版开机后网络无法自动链接 麒麟系统开机没有连接ens33
  • vue+onlyOffice+java : 集成在线编辑word并保存
  • linux上用Jmter进行压测
  • 【Java代码审计】代码审计的方法及常用工具
  • 我国吻合器市场规模不断扩大 国产化率有所增长
  • 深度剖析Comate智能产品:科技巧思,实用至上
  • Centos 7.9 配置VNCServer实现远程vnc连接
  • 设计模式-08 - 模板方法模式 Template Method
  • Android 适配阿拉伯语之vector图标镜像
  • 推荐4个可用的github国内镜像
  • 从项目开始学习Vue——02(若依框架)
  • 使用JavaScript日历小部件和DHTMLX Gantt的应用场景(二)
  • springboot整合redis多数据源(附带RedisUtil)
  • Web实时通信的学习之旅:SSE(Server-Sent Events)的技术详解及简单示例演示
  • Apache Flume事务
  • 根据部门id删除该部门下的员工(事务)
  • Java之String类
  • es终止快照恢复进程的方法
  • ubantu安装rabbbitmq
  • 了解 条码工具 Dynamsoft 在条码读取器中的形态运算
  • NIO和NIO.2对比
  • Google准备好了吗?OpenAI发布ChatGPT驱动搜索引擎|TodayAI