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

C++仿函数

在C++中,我们经常需要对类中的元素进行比较,例如在排序、查找等操作中。为了使类更加灵活,我们可以通过自定义比较函数来实现不同的比较方式。在本文中,我们将探讨如何在类中使用仿函数和 Lambda 表达式来定义自定义比较函数。

1. 仿函数(Functor)的使用

仿函数是一个类对象,它的实例可以像函数一样被调用。我们可以通过重载函数调用运算符 operator() 来实现仿函数。以下是一个简单的示例,演示了如何在类中使用仿函数来实现自定义比较:

解释

#include <iostream>// 定义一个比较类
class MyComparator {
public:// 重载函数调用运算符bool operator()(const int& a, const int& b) const {return a > b; // 自定义比较规则}
};// 使用比较类的类模板
template <typename T, typename Compare = MyComparator>
class MyClass {
public:// 成员变量T data;// 构造函数,接受比较类作为参数MyClass(const T& d, const Compare& cmp = Compare()) : data(d), comparator(cmp) {}// 比较函数bool compare(const T& other) const {return comparator(data, other);}private:Compare comparator; // 保存比较类的实例
};int main() {// 创建类实例,使用默认比较规则MyClass<int> myObj(5);// 使用比较函数std::cout << myObj.compare(3) << std::endl;  // 输出 1(true)// 创建类实例,使用自定义比较规则MyClass<int, MyComparator> customObj(5);// 使用比较函数std::cout << customObj.compare(8) << std::endl;  // 输出 0(false)return 0;
}

2. Lambda 表达式的使用

Lambda 表达式是C++11引入的一种方便的匿名函数表示方法。它允许我们在需要函数的地方内联定义函数,包括自定义比较函数。以下是如何在类中使用 Lambda 表达式的示例:

解释

#include <iostream>
#include <functional>// 使用 Lambda 表达式的类模板
template <typename T>
class MyClassWithLambda {
public:// 成员变量T data;// 构造函数,接受 Lambda 表达式作为参数MyClassWithLambda(const T& d, const std::function<bool(const T&, const T&)>& cmp) : data(d), comparator(cmp) {}// 比较函数bool compare(const T& other) const {return comparator(data, other);}private:std::function<bool(const T&, const T&)> comparator; // 保存 Lambda 表达式
};int main() {// 创建类实例,使用 Lambda 表达式定义比较规则MyClassWithLambda<int> lambdaObj(5, [](const int& a, const int& b) {return a > b;});// 使用比较函数std::cout << lambdaObj.compare(3) << std::endl;  // 输出 1(true)return 0;
}

或者

解释

#include <iostream>// 使用比较类的类模板
template <typename T, typename Compare = std::less<T>>
class MyClass {
public:// 成员变量T data;// 构造函数,接受比较类作为参数MyClass(const T& d, const Compare& cmp = Compare()) : data(d), comparator(cmp) {}// 比较函数bool compare(const T& other) const {return comparator(data, other);}private:Compare comparator; // 保存比较类的实例
};int main() {auto MyComparator = [] (const int& x, const int& y) {return x > y;};// 创建类实例,使用自定义比较规则MyClass<int, decltype(MyComparator)> customObj(5, MyComparator);// 使用比较函数std::cout << customObj.compare(8) << std::endl;  // 输出 0(false)return 0;
}
http://www.lryc.cn/news/403706.html

相关文章:

  • 文献阅读:tidyomics 生态系统:增强组学数据分析
  • MySQL运维实战之Clone插件(10.1)使用Clone插件
  • 【系统架构设计】数据库系统(三)
  • 免费视频批量横版转竖版
  • 内存管理(知识点)
  • 【雷丰阳-谷粒商城 】【分布式高级篇-微服务架构篇】【29】Sentinel
  • 防范UDP Flood攻击的策略与实践
  • 昇思25天学习打卡营第17天 | CycleGAN图像风格迁移互换
  • Leetcode 2520. 统计能整除数字的位数
  • WEB前端08-综合案例(动态表格)
  • 【面试题】Redo log和Undo log
  • 开发实战经验分享:互联网医院系统源码与在线问诊APP搭建
  • springboot系列教程(十六):配置Actuator组件,实现系统监控
  • 单臂路由组网实验,单臂路由的定义、适用情况、作用
  • 【数据结构初阶】顺序表三道经典算法题(详解+图例)
  • SpringBoot接入JPA连接数据库H2或MySQL例子
  • 持续集成05--Gogs的安装与使用
  • C++--fill
  • Java:对比一个对象更新前后具体被修改了哪些值
  • GO——GMP 好文整理
  • 园区AR导航系统构建详解:从三维地图构建到AR融合导航的实现
  • 接口测试总结(非标准)
  • 在Ubuntu 18.04上安装和使用Composer的方法
  • ssm 学习 ---(spring)
  • Jupyter Notebook安装及基本使用
  • Jenkins+Maven+Gitlab+Tomcat自动化构建打包+部署
  • Synchronized升级到重量级锁会发生什么?
  • 【Webpack】HMR 热更新
  • 【计算机视觉】siamfc论文复现
  • PotatoTool 蓝队版 V1.3 发布:增强功能和性能优化