STL中的 函数对象

目录

1 函数对象

1. 基本概念

1.2 使用

2 谓词

2.1 基本概念

2.2 一元谓词

2.3 二元谓词

3 内建函数对象

3.1 基本概念

3.2 算数仿函数

3.3 关系仿函数

3.4 逻辑仿函数


1 函数对象

1. 基本概念

概念

  • ①重载函数调用操作符的类,其对象常称为函数对象

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

本质

  • 函数对象(仿函数)本身是一个类,并非一个函数

1.2 使用

特点

  • 函数对象在使用时 ,可以像普通函数那样调用,可以有参数与返回值

  • 函数对象超出普通函数的概念,可以有自己的状态

  • 函数对象可以作为参数传递

测试

  • ①仿函数MyAdd

 class MyAdd
 {
 public:
   int operator()(int x,int y)
   {
     return x + y;
   }
 };

test函数,直接调用普通函数那样调用

 void test01()
 {
   MyAdd myadd;
   cout << myadd(1, 5) << endl;
 }

img

  • ②仿函数MyPrint,内有属性count来记录自己状态

 class MyPrint
 {
 public:
   MyPrint()
   {
     this->count = 0; // 初始化为0
   }
   void  operator()(string s)
   {
     cout << s << endl;
     this->count++;
   }
   int count; // 记录自己状态
 };

test函数,调用多次MyPrint函数

 void test02()
 {
   MyPrint myprint;
   myprint("Jocye is my ba");  myprint("Jocye is my ba");
   myprint("Jocye is my ba");  myprint("Jocye is my ba");
   cout << myprint.count << endl;
 }

img

  • doPrint函数

 void doPrint(MyPrint& mp,string s) // mp作为一个参数进行传递
 {
   mp(s); // 同时还能使用自身来打印s
 }

test函数,我们调用doPrint函数

 void test03()
 {
   MyPrint myprint;
   doPrint(myprint, "Hello my");
 }

img

2 谓词

2.1 基本概念

概念:返回bool类型的仿函数称为谓词

  • 如果operator()接受一个参数,称为一元谓词

  • 如果operator()接受两个参数,称为二元谓词

2.2 一元谓词

 void test04()
 {
   vector <int> v;
   int i = 10;
   while (i--)
   {
     v.push_back(i);
   }
   // 上面GreateFive仿函数,正常应该先创建一个对象,下面再使用
   // 这里我直接使用匿名对象GreateFive();
   vector<int>::iterator it = find_if(v.begin(), v.end(), GreateFive());
   if (it != v.end())
     cout << "有了:" << *it;
   else
     cout << "没有" << endl;
 }

find_if(起始迭代器,终止迭代器,仿函数); 返回的也是个迭代器,找到返回所在位置迭代器,否则返回end();

  • 仿函数GreateFive(),大于5返回真

 class GreateFive
 {
 public:
   bool operator()(int val)
   {
     return val > 5;
   }
 };

img

由于是从9加进去,加到1,因此第一个数9就满足,返回

2.3 二元谓词

 void test05()
 {
   vector<int> v;
   v.push_back(10);  v.push_back(40);  v.push_back(20);
   v.push_back(30);  v.push_back(50);  v.push_back(60);
   sort(v.begin(),v.end());
   printv(v);
 ​
   // 使用函数对象MyCompare()使其实现降序
   sort(v.begin(), v.end(), MyCompare());
   printv(v);
 }

二元谓词MyCompare

 class MyCompare
 {
 public:
   bool operator()(int x, int y) // 二元谓词,2个参数
   {
     return x > y;
   }
 };

打印函数printv()

 // 二元谓词
 void printv(vector<int>& v)
 {
   for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
   {
     cout << *it << " ";
   }
   cout << endl;
 }

img

3 内建函数对象

3.1 基本概念

首先,STL内建了一些函数对象(仿函数)

分类

  • 算数仿函数

  • 关系仿函数

  • 逻辑仿函数

使用

  • 这些仿函数所产生的对象,用法与一般函数完全相同

  • 使用内建函数对象,需要引用头文件#include<functional>

3.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>  取反

其中negate是一元运算,其他都是二元运算

测试

  • ①取反仿函数

 void test06()
 { 
   negate<int> n;
   cout << n(50) << endl;
 }

img

  • ②加法仿函数

 void test07()
 {
   plus<int> p; // 默认传同种数据类型
   cout << p(5,10) << endl;
 }

img

3.3 关系仿函数

功能:实现关系对比

函数

 //  关系仿函数
 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>   小于等于

测试

在此之前,我们创建vector容器里面存入数据,如果想让数据降序排序,需要自己实现仿函数

 void test08()
 {
   vector<int> v;
   v.push_back(10);  v.push_back(40);  v.push_back(20);
   v.push_back(30);  v.push_back(50);  v.push_back(60);
   sort(v.begin(), v.end());
   printv(v);
 ​
   // 使用函数对象MyCompare()使其实现降序
   sort(v.begin(), v.end(), MyCompare());
   printv(v);
 }

此时,我们使用内建函数greater

 // 使用内建函数对象
   sort(v.begin(), v.end(), greater<int>());

img

而若是想比较其他的数据,如float,我们只需要修改为greater<float>();即可


同时,greater是比较常用的

我们打开sort排序源码

img

img

可以发现,如果要重载,上面有3个参数,而下面默认的只有2个参数(起始和终止区间)

img

而默认的2个参数的,调用的是less<>,因此默认使用升序排序从小到大

3.4 逻辑仿函数

功能:实现逻辑运算

函数

 //  逻辑仿函数
 template<class T> bool logical_and<T> 逻辑与,同时为真才真,其余为假
 template<class T> bool logical_or<T>  逻辑或,同时为假才假,其余为真
 template<class T> bool logical_not<T> 逻辑非,取反操作

测试

  • 创建容器v和容器v2v中存入truefalse的数据,取反后存入v2

 void test09()
 {
   vector<int > v;
   v.push_back(true);  v.push_back(false); v.push_back(true);
   v.push_back(true);  v.push_back(false);
   printv(v);
 ​
   // 取反,放入另一个容器
   vector<int> v2;
   v2.resize(v.size()); // 指定v2的size为v的size
   transform(v.begin(), v.end(), v2.begin(),logical_not<int>());
   //搬运算法   起始      终止    目标的起始   逻辑非
   printv(v2);
 }

img

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值