C++ 中的仿函数 functor

2023-10-29 21:12
文章标签 c++ 函数 functor

本文主要是介绍C++ 中的仿函数 functor,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一   仿函数的概念

1. 定义

  仿函数(functor)是一种使用上像函数的类,其本质是一个实现了 operato() 函数的类,这种类就有了类似于函数一样的使用行为,这就是仿函数的类。

仿函数在 C++ STL标准库中被大量使用。

2. 特点

1. 仿函数是一个类,不是一个函数

2. 仿函数的类需要重载 operator() 函数,以此拥有函数的行为

二  STL 中常见的 仿函数介绍

1. 基类

  1.1  unary_function

 /***  This is one of the @link functors functor base classes@endlink.*/template<typename _Arg, typename _Result>struct unary_function{/// @c argument_type is the type of the argumenttypedef _Arg 	argument_type;   /// @c result_type is the return typetypedef _Result 	result_type;  };

   作为拥有一个输入参数的仿函数常用基类,该类主要回答了两个问题:

     1. 该仿函数类的输入参数是什么类型:argument_type

     2. 该仿函数类的返回参数是什么类型: result_type

  1.2  binary_function

 /***  This is one of the @link functors functor base classes@endlink.*/template<typename _Arg1, typename _Arg2, typename _Result>struct binary_function{/// @c first_argument_type is the type of the first argumenttypedef _Arg1 	first_argument_type; /// @c second_argument_type is the type of the second argumenttypedef _Arg2 	second_argument_type;/// @c result_type is the return typetypedef _Result 	result_type;};

作为拥有两个输入参数的仿函数常用基类,该类主要回答了两个问题:

     1. 该仿函数类的输入参数是什么类型:first_argument_type 与 second_argument_type

     2. 该仿函数类的返回参数是什么类型: result_type

2.  STL 中常见仿函数

2.1  plus

 /// One of the @link arithmetic_functors math functors@endlink.template<typename _Tp>struct plus : public binary_function<_Tp, _Tp, _Tp>{_Tpoperator()(const _Tp& __x, const _Tp& __y) const{ return __x + __y; }};

2.2 minus

/// One of the @link arithmetic_functors math functors@endlink.template<typename _Tp>struct minus : public binary_function<_Tp, _Tp, _Tp>{_Tpoperator()(const _Tp& __x, const _Tp& __y) const{ return __x - __y; }};

2.3  multiplies

  /// One of the @link arithmetic_functors math functors@endlink.template<typename _Tp>struct multiplies : public binary_function<_Tp, _Tp, _Tp>{_Tpoperator()(const _Tp& __x, const _Tp& __y) const{ return __x * __y; }};

2.4  divides

template<typename _Tp>struct divides : public binary_function<_Tp, _Tp, _Tp>{_Tpoperator()(const _Tp& __x, const _Tp& __y) const{ return __x / __y; }};

2.5  equal_to

 /// One of the @link comparison_functors comparison functors@endlink.template<typename _Tp>struct equal_to : public binary_function<_Tp, _Tp, bool>{booloperator()(const _Tp& __x, const _Tp& __y) const{ return __x == __y; }};

2.6  less

 /// One of the @link comparison_functors comparison functors@endlink.template<typename _Tp>struct less : public binary_function<_Tp, _Tp, bool>{booloperator()(const _Tp& __x, const _Tp& __y) const{ return __x < __y; }};

2.7  greater

/// One of the @link comparison_functors comparison functors@endlink.template<typename _Tp>struct greater : public binary_function<_Tp, _Tp, bool>{booloperator()(const _Tp& __x, const _Tp& __y) const{ return __x > __y; }};

2.8   _Select1st

template<typename _Pair>struct _Select1st: public unary_function<_Pair, typename _Pair::first_type>{typename _Pair::first_type&operator()(_Pair& __x) const{ return __x.first; }const typename _Pair::first_type&operator()(const _Pair& __x) const{ return __x.first; }};

2.9  _Select2nd

template<typename _Pair>struct _Select2nd: public unary_function<_Pair, typename _Pair::second_type>{typename _Pair::second_type&operator()(_Pair& __x) const{ return __x.second; }const typename _Pair::second_type&operator()(const _Pair& __x) const{ return __x.second; }};

3. 使用例子

#include<algorithm>
#include<iostream>int main()
{// 1. plusstd::cout << "------ plus ------" << std::endl;std::vector<int>  vec = {1, 3, 5};std::plus<int> pl;int init = 0;int res1 = std::accumulate(vec.begin(), vec.end(), init, pl);std::cout << res1 << std::endl;std::cout << "------ plus ------" << std::endl; // 9// 2. minusstd::cout << "------ minus ------" << std::endl;init = 10;std::minus<int> mis;int res2 =  std::accumulate(vec.begin(), vec.end(), init, mis);std::cout << res2 << std::endl; // 1std::cout << "------ minus ------" << std::endl;// 3. multiesstd::cout << "------ multies ------" << std::endl;init = 1;std::multiplies<int> multiply;int res3 = std::accumulate(vec.begin(), vec.end(), init, multiply);std::cout << res3 << std::endl; // 15std::cout << "------ multies ------" << std::endl;// 4. dividesstd::cout << "------ divides ------" << std::endl;init = 90;std::divides<int> divid;int res4 = std::accumulate(vec.begin(), vec.end(), init, divid);std::cout << res4 << std::endl; // 6std::cout << "------ divides ------" << std::endl;// 5. equal_tostd::cout << "------ equal_to ------" << std::endl;std::pair<int, std::string> pair1 = std::make_pair(1, "abc");std::pair<int, std::string> pair2 = std::make_pair(2, "abc");std::equal_to<std::string> equal;std::_Select2nd<std::pair<int, std::string>> second_argu;std::cout << equal(second_argu(pair1), second_argu(pair2)) << std::endl;// 1std::cout << "------ equal_to ------" << std::endl;// 6. lessstd::cout << "------ less ------" << std::endl;std::less<int> less;std::cout << less(3, 6) << std::endl; // 1std::cout << "------ less ------" << std::endl;// 7. greaterstd::cout << "------ greater ------" << std::endl;std::greater<int> greater;std::cout << greater(3, 6) << std::endl; // 0std::cout << "------ greater ------" << std::endl;// 8. _Select1ststd::cout << "------ _Select1st ------" << std::endl;std::pair<int, std::string> pair3 = std::make_pair(1, "abc");std::_Select1st<std::pair<int, std::string>> select1st;std::cout << select1st(pair3) << std::endl; // 1std::cout << "------ _Select1st ------" << std::endl;// 9. _Select2ndstd::cout << "------ _Select2nd ------" << std::endl;std::pair<int, std::string> pair4 = std::make_pair(1, "abc");std::_Select2nd<std::pair<int, std::string>> select2nd;std::cout << select2nd(pair3) << std::endl; // abcstd::cout << "------ _Select2nd ------" << std::endl;return 0;
}

输出:

这篇关于C++ 中的仿函数 functor的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

C++中RAII资源获取即初始化

《C++中RAII资源获取即初始化》RAII通过构造/析构自动管理资源生命周期,确保安全释放,本文就来介绍一下C++中的RAII技术及其应用,具有一定的参考价值,感兴趣的可以了解一下... 目录一、核心原理与机制二、标准库中的RAII实现三、自定义RAII类设计原则四、常见应用场景1. 内存管理2. 文件操

C++中零拷贝的多种实现方式

《C++中零拷贝的多种实现方式》本文主要介绍了C++中零拷贝的实现示例,旨在在减少数据在内存中的不必要复制,从而提高程序性能、降低内存使用并减少CPU消耗,零拷贝技术通过多种方式实现,下面就来了解一下... 目录一、C++中零拷贝技术的核心概念二、std::string_view 简介三、std::stri

C++高效内存池实现减少动态分配开销的解决方案

《C++高效内存池实现减少动态分配开销的解决方案》C++动态内存分配存在系统调用开销、碎片化和锁竞争等性能问题,内存池通过预分配、分块管理和缓存复用解决这些问题,下面就来了解一下... 目录一、C++内存分配的性能挑战二、内存池技术的核心原理三、主流内存池实现:TCMalloc与Jemalloc1. TCM

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

C++ 函数 strftime 和时间格式示例详解

《C++函数strftime和时间格式示例详解》strftime是C/C++标准库中用于格式化日期和时间的函数,定义在ctime头文件中,它将tm结构体中的时间信息转换为指定格式的字符串,是处理... 目录C++ 函数 strftipythonme 详解一、函数原型二、功能描述三、格式字符串说明四、返回值五

C++作用域和标识符查找规则详解

《C++作用域和标识符查找规则详解》在C++中,作用域(Scope)和标识符查找(IdentifierLookup)是理解代码行为的重要概念,本文将详细介绍这些规则,并通过实例来说明它们的工作原理,需... 目录作用域标识符查找规则1. 普通查找(Ordinary Lookup)2. 限定查找(Qualif

Python中bisect_left 函数实现高效插入与有序列表管理

《Python中bisect_left函数实现高效插入与有序列表管理》Python的bisect_left函数通过二分查找高效定位有序列表插入位置,与bisect_right的区别在于处理重复元素时... 目录一、bisect_left 基本介绍1.1 函数定义1.2 核心功能二、bisect_left 与

java中BigDecimal里面的subtract函数介绍及实现方法

《java中BigDecimal里面的subtract函数介绍及实现方法》在Java中实现减法操作需要根据数据类型选择不同方法,主要分为数值型减法和字符串减法两种场景,本文给大家介绍java中BigD... 目录Java中BigDecimal里面的subtract函数的意思?一、数值型减法(高精度计算)1.

C/C++ chrono简单使用场景示例详解

《C/C++chrono简单使用场景示例详解》:本文主要介绍C/C++chrono简单使用场景示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友... 目录chrono使用场景举例1 输出格式化字符串chrono使用场景China编程举例1 输出格式化字符串示