【C++函数对象】STL基础语法学习 | 仿函数&谓词&内建仿函数

简介: 重载函数调用操作符的类,其对象常称为函数对象。函数对象使用重载的()时,行为类似函数的调用,所以也叫仿函数。它的本质为一个类,而不是一个函数。

●仿函数


1.概念

       重载函数调用操作符的类,其对象常称为函数对象。函数对象使用重载的()时,行为类似函数的调用,所以也叫仿函数。它的本质为一个类,而不是一个函数。


2.使用

       功能特点:


               1.仿函数在使用时可以像普通函数那样调用,可以有参数和返回值

#include<iostream>
using namespace std;
//仿函数在使用时可以像普通函数那样调用,可以有参数和返回值
class add {
public:
  int operator()(const int value1, const int value2)
  {
  return value1 + value2;
  }
};
class sub {
public:
  int operator()(const int value1, const int value2)
  {
  return value1 - value2;
  }
};
class mul {
public:
  double operator()(const double value1,const double value2)
  {
  return value1 * value2;
  }
};
void text()
{
  add ad;
  cout << "相加:" << ad(10, 20) << endl;
  sub sb;
  cout << "相减:" << sb(10, 20) << endl;
  mul ml;
  cout << "相乘:" << ml(5.5,7) << endl;
}
int main()
{
  text();
}

5a3208372e8c40a9ae8684d46e62243e_3627ae01cab6436aa9d866dcc76a1630.png

               2.仿函数不同于普通函数的概念,它可以有自己的状态


#include<iostream>
using namespace std;
class print {
public:
  void operator()(const int value1,const int value2)//仿函数不同于普通函数的概念,它可以有自己的状态
  {
  cout << "相加:" << value1 + value2 << endl;
  cout << "相减:" << value1 - value2 << endl;
  cout << "相乘:" << value1 * value2 << endl;
  }
};
void text()
{
  print pt;
  pt(10,20);
}
int main()
{
  text();
}

867880a1633172f3182729ae018b5db6_4b04d767bfda40c486d5375267e2fd56.png

               3.仿函数可以作为参数传递

#include<iostream>
using namespace std;
class print {
public:
  void operator()(const int value1, const int value2)
  {
  cout << "相加:" << value1 + value2 << endl;
  cout << "相减:" << value1 - value2 << endl;
  cout << "相乘:" << value1 * value2 << endl;
  }
};
void scanf(print &pt)//仿函数可以作为参数传递
{
  pt(10,20);
}
void text()
{
  print pt;
  scanf(pt);
}
int main()
{
  text();
}

db5a21819d48cb8178fc85aa4f25cd26_b964328ecf934c738ce2f4a77b7d4cc6.png


●谓词


1.一元谓词

               如果仿函数的返回值为bool类型并且operator()接受一个参数,则称它为一元谓词

#include<iostream>
#include<algorithm>  //STL算法头文件定义
#include<vector>
using namespace std;
class unitary {
public:
  bool operator()(const int value)  //一元仿函数
  {
  return value == 20;
  }
};
void text()
{
  vector<int>v;
  for (int i = 1, j = 10; i <= 10; i++, j += 10)
  {
  v.push_back(j);
  }
  //10 20 30 40 50 60 70 80 90 100
  vector<int>::iterator p = find_if(v.begin(), v.end(), unitary());  
  //利用find_if这个算法,在一元仿函数中去判断vector容器中是否有元素20
  if (p == v.end())
  cout << "未找到" << endl;
  else
  cout << "从vector容器中找到了值为20的元素" << endl;
}
int main()
{
  text();
}

c2b8fd36f080532dcd42462058c1c5db_465e0f5a69464b05b92540e2d87f8212.png


2.二元谓词

               如果仿函数的返回值为bool类型并且operator()接受两个参数,则称它为二元谓词

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void printvector(vector<int>&v)
{
  for (vector<int>::iterator i = v.begin(); i != v.end(); i++)
  {
  cout << *i<<" ";
  }
  cout << endl;
}
class compare {
public:
  bool operator()(const int value1,const int value2)  //二元仿函数
  {
  return value1 > value2;
  }
};
void text()
{
  vector<int>v;
  v.push_back(45);
  v.push_back(10);
  v.push_back(9);
  v.push_back(67);
  v.push_back(35);
  //45 10 9 67 35
  cout << "从小到大排序:";
  sort(v.begin(), v.end());
  //9 10 35 45 67
  printvector(v);
  cout << "从大到小排序:";
  sort(v.begin(), v.end(), compare());
  //67 45 35 10 9
  printvector(v);
}
int main()
{
  text();
}

4853745bebc5fa07cb8880db31935b05_33ae1e3646ba4c45b5fad7d23364d74f.png


●内建仿函数


1.算数仿函数

函数原型:


      ■template T plus //加法仿函数


      ■template T minus //减法仿函数


      ■template T multiplies //乘法仿函数


      ■template T divides //除法仿函数


      ■template T modulus //取模仿函数


      ■template T negate //取反仿函数


#include<iostream>
#include<functional>  //内建仿函数头文件定义
using namespace std;
void text()
{
  //相加仿函数
  plus<int>p;
  cout << "相加:" << p(10, 20) << endl;
  //相减仿函数
  minus<float>mi;
  cout << "相减:" << mi(3.18,2.90) << endl;
  //乘法仿函数
  multiplies<double>mu;
  cout << "相乘:" << mu(9.19, 3.14) << endl;
  //除法仿函数
  divides<double>di;
  cout << "相除:" << di(25.75, 3.15) << endl;
  //取模仿函数
  modulus<int>mo;
  cout << "取模:" << mo(9,4) << endl;
  //取反仿函数
  negate<int>n;
  cout << "取反:" << n(1) << endl;
}
int main()
{
  text();
}

f0d238cd286ff698be1058f77b09d7f3_7205fd1bb5b441389fcc960ec01376c9.png


2.关系仿函数

函数原型:

       ■template bool equal to         //等于


       ■template bool not equal to         //不等于


       ■template bool greater         //大于


       ■template bool greater_equal         //大于等于


       ■template bool less         //小于


       ■template bool less_equal         //小于等于

#include<iostream>
#include<algorithm>
#include<functional>
#include<vector>
using namespace std;
void printvector(vector<int>&v)
{
  for (vector<int>::iterator i = v.begin(); i != v.end(); i++)
  {
  cout << *i<<" ";
  }
  cout << endl;
}
void text()
{
  vector<int>v;
  cout << "请向vector容器中输入元素:" << endl;;
  for (int i = 1; i <= 10; i++)
  {
  int elem; cin>>elem;
  v.push_back(elem);
  }
  //常用关系仿函数
  //大于=降序
  sort(v.begin(),v.end(),greater<int>());
  printvector(v);
  //小于=升序
  sort(v.begin(), v.end(), less<int>());
  printvector(v);
}
int main()
{
  text();
}

45593bec64f5724f94c9f325f478fe12_883c935be5f847c0a9e190cc5d5099f0.png


3.逻辑仿函数

函数原型:(该仿函数基本不用,所以下面代码中只做简单了解)


       ■template bool logical and         //逻辑与


       ■template bool logical or         //逻辑或


       ■template bool logical not         //逻辑非


#include<iostream>
#include<vector>
#include<functional>
#include<algorithm>
using namespace std;
void printvector(vector<bool>&v)
{
  for (vector<bool>::iterator i = v.begin(); i != v.end(); i++)
  {
  cout << *i<<" ";
  }
  cout << endl;
}
void text()
{
  vector<bool>v;
  v.push_back(true);
  v.push_back(false);
  cout << "初始状态:" << endl;
  printvector(v);
  vector<bool>v1;
  v1.resize(v.size());
  transform(v.begin(), v.end(), v1.begin(), logical_not<bool>());
  cout << "逻辑非后的状态:" << endl;
  printvector(v1);
}
int main()
{
  text();
}

4583e82ab5553632bb7f60a0d4fe1d6d_5a36ed05b14743f78533281b6d1a5854.png

目录
相关文章
|
编译器 C++ 容器
【c++丨STL】基于红黑树模拟实现set和map(附源码)
本文基于红黑树的实现,模拟了STL中的`set`和`map`容器。通过封装同一棵红黑树并进行适配修改,实现了两种容器的功能。主要步骤包括:1) 修改红黑树节点结构以支持不同数据类型;2) 使用仿函数适配键值比较逻辑;3) 实现双向迭代器支持遍历操作;4) 封装`insert`、`find`等接口,并为`map`实现`operator[]`。最终,通过测试代码验证了功能的正确性。此实现减少了代码冗余,展示了模板与仿函数的强大灵活性。
529 2
|
缓存 算法 程序员
C++STL底层原理:探秘标准模板库的内部机制
🌟蒋星熠Jaxonic带你深入STL底层:从容器内存管理到红黑树、哈希表,剖析迭代器、算法与分配器核心机制,揭秘C++标准库的高效设计哲学与性能优化实践。
1072 3
C++STL底层原理:探秘标准模板库的内部机制
|
存储 编译器 开发工具
01.C++基础语法
本教程系统讲解C++基础语法,涵盖语言特性、编译器使用、命名空间、头文件、修饰符与关键字等核心内容,助你快速掌握C++编程基础。
664 9
|
存储 算法 C++
【c++丨STL】map/multimap的使用
本文详细介绍了STL关联式容器中的`map`和`multimap`的使用方法。`map`基于红黑树实现,内部元素按键自动升序排列,存储键值对,支持通过键访问或修改值;而`multimap`允许存在重复键。文章从构造函数、迭代器、容量接口、元素访问接口、增删操作到其他操作接口全面解析了`map`的功能,并通过实例演示了如何用`map`统计字符串数组中各元素的出现次数。最后对比了`map`与`set`的区别,强调了`map`在处理键值关系时的优势。
1033 73
|
存储 缓存 C++
C++ 容器全面剖析:掌握 STL 的奥秘,从入门到高效编程
C++ 标准模板库(STL)提供了一组功能强大的容器类,用于存储和操作数据集合。不同的容器具有独特的特性和应用场景,因此选择合适的容器对于程序的性能和代码的可读性至关重要。对于刚接触 C++ 的开发者来说,了解这些容器的基础知识以及它们的特点是迈向高效编程的重要一步。本文将详细介绍 C++ 常用的容器,包括序列容器(`std::vector`、`std::array`、`std::list`、`std::deque`)、关联容器(`std::set`、`std::map`)和无序容器(`std::unordered_set`、`std::unordered_map`),全面解析它们的特点、用法
1229 1
C++ 容器全面剖析:掌握 STL 的奥秘,从入门到高效编程
|
存储 算法 C++
【c++丨STL】set/multiset的使用
本文深入解析了STL中的`set`和`multiset`容器,二者均为关联式容器,底层基于红黑树实现。`set`支持唯一性元素存储并自动排序,适用于高效查找场景;`multiset`允许重复元素。两者均具备O(logN)的插入、删除与查找复杂度。文章详细介绍了构造函数、迭代器、容量接口、增删操作(如`insert`、`erase`)、查找统计(如`find`、`count`)及`multiset`特有的区间操作(如`lower_bound`、`upper_bound`、`equal_range`)。最后预告了`map`容器的学习,其作为键值对存储的关联式容器,同样基于红黑树,具有高效操作特性。
906 3
|
编译器 C++
类和对象(中 )C++
本文详细讲解了C++中的默认成员函数,包括构造函数、析构函数、拷贝构造函数、赋值运算符重载和取地址运算符重载等内容。重点分析了各函数的特点、使用场景及相互关系,如构造函数的主要任务是初始化对象,而非创建空间;析构函数用于清理资源;拷贝构造与赋值运算符的区别在于前者用于创建新对象,后者用于已存在的对象赋值。同时,文章还探讨了运算符重载的规则及其应用场景,并通过实例加深理解。最后强调,若类中存在资源管理,需显式定义拷贝构造和赋值运算符以避免浅拷贝问题。
|
存储 编译器 C++
类和对象(上)(C++)
本篇内容主要讲解了C++中类的相关知识,包括类的定义、实例化及this指针的作用。详细说明了类的定义格式、成员函数默认为inline、访问限定符(public、protected、private)的使用规则,以及class与struct的区别。同时分析了类实例化的概念,对象大小的计算规则和内存对齐原则。最后介绍了this指针的工作机制,解释了成员函数如何通过隐含的this指针区分不同对象的数据。这些知识点帮助我们更好地理解C++中类的封装性和对象的实现原理。
|
存储 算法 C++
【c++丨STL】priority_queue(优先级队列)的使用与模拟实现
本文介绍了STL中的容器适配器`priority_queue`(优先级队列)。`priority_queue`根据严格的弱排序标准设计,确保其第一个元素始终是最大元素。它底层使用堆结构实现,支持大堆和小堆,默认为大堆。常用操作包括构造函数、`empty`、`size`、`top`、`push`、`pop`和`swap`等。我们还模拟实现了`priority_queue`,通过仿函数控制堆的类型,并调用封装容器的接口实现功能。最后,感谢大家的支持与关注。
1174 1
|
编译器 C++
类和对象(下)C++
本内容主要讲解C++中的初始化列表、类型转换、静态成员、友元、内部类、匿名对象及对象拷贝时的编译器优化。初始化列表用于成员变量定义初始化,尤其对引用、const及无默认构造函数的类类型变量至关重要。类型转换中,`explicit`可禁用隐式转换。静态成员属类而非对象,受访问限定符约束。内部类是独立类,可增强封装性。匿名对象生命周期短,常用于临时场景。编译器会优化对象拷贝以提高效率。最后,鼓励大家通过重复练习提升技能!