【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

目录
相关文章
|
1天前
|
存储 缓存 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`),全面解析它们的特点、用法
C++ 容器全面剖析:掌握 STL 的奥秘,从入门到高效编程
|
1天前
|
存储 算法 C++
深入浅出 C++ STL:解锁高效编程的秘密武器
C++ 标准模板库(STL)是现代 C++ 的核心部分之一,为开发者提供了丰富的预定义数据结构和算法,极大地提升了编程效率和代码的可读性。理解和掌握 STL 对于 C++ 开发者来说至关重要。以下是对 STL 的详细介绍,涵盖其基础知识、发展历史、核心组件、重要性和学习方法。
|
1天前
|
编译器 C++ 开发者
【C++篇】深度解析类与对象(下)
在上一篇博客中,我们学习了C++的基础类与对象概念,包括类的定义、对象的使用和构造函数的作用。在这一篇,我们将深入探讨C++类的一些重要特性,如构造函数的高级用法、类型转换、static成员、友元、内部类、匿名对象,以及对象拷贝优化等。这些内容可以帮助你更好地理解和应用面向对象编程的核心理念,提升代码的健壮性、灵活性和可维护性。
|
1天前
|
安全 编译器 C语言
【C++篇】深度解析类与对象(中)
在上一篇博客中,我们学习了C++类与对象的基础内容。这一次,我们将深入探讨C++类的关键特性,包括构造函数、析构函数、拷贝构造函数、赋值运算符重载、以及取地址运算符的重载。这些内容是理解面向对象编程的关键,也帮助我们更好地掌握C++内存管理的细节和编码的高级技巧。
|
1天前
|
存储 程序员 C语言
【C++篇】深度解析类与对象(上)
在C++中,类和对象是面向对象编程的基础组成部分。通过类,程序员可以对现实世界的实体进行模拟和抽象。类的基本概念包括成员变量、成员函数、访问控制等。本篇博客将介绍C++类与对象的基础知识,为后续学习打下良好的基础。
|
4天前
|
编译器 C语言 C++
类和对象的简述(c++篇)
类和对象的简述(c++篇)
|
26天前
|
C++ 容器
【c++丨STL】stack和queue的使用及模拟实现
本文介绍了STL中的两个重要容器适配器:栈(stack)和队列(queue)。容器适配器是在已有容器基础上添加新特性或功能的结构,如栈基于顺序表或链表限制操作实现。文章详细讲解了stack和queue的主要成员函数(empty、size、top/front/back、push/pop、swap),并提供了使用示例和模拟实现代码。通过这些内容,读者可以更好地理解这两种数据结构的工作原理及其实现方法。最后,作者鼓励读者点赞支持。 总结:本文深入浅出地讲解了STL中stack和queue的使用方法及其模拟实现,帮助读者掌握这两种容器适配器的特性和应用场景。
56 21
|
1月前
|
C++ 芯片
【C++面向对象——类与对象】Computer类(头歌实践教学平台习题)【合集】
声明一个简单的Computer类,含有数据成员芯片(cpu)、内存(ram)、光驱(cdrom)等等,以及两个公有成员函数run、stop。只能在类的内部访问。这是一种数据隐藏的机制,用于保护类的数据不被外部随意修改。根据提示,在右侧编辑器补充代码,平台会对你编写的代码进行测试。成员可以在派生类(继承该类的子类)中访问。成员,在类的外部不能直接访问。可以在类的外部直接访问。为了完成本关任务,你需要掌握。
70 19
|
1月前
|
存储 编译器 数据安全/隐私保护
【C++面向对象——类与对象】CPU类(头歌实践教学平台习题)【合集】
声明一个CPU类,包含等级(rank)、频率(frequency)、电压(voltage)等属性,以及两个公有成员函数run、stop。根据提示,在右侧编辑器补充代码,平台会对你编写的代码进行测试。​ 相关知识 类的声明和使用。 类的声明和对象的声明。 构造函数和析构函数的执行。 一、类的声明和使用 1.类的声明基础 在C++中,类是创建对象的蓝图。类的声明定义了类的成员,包括数据成员(变量)和成员函数(方法)。一个简单的类声明示例如下: classMyClass{ public: int
52 13
|
1月前
|
C++ 开发者
C++学习之继承
通过继承,C++可以实现代码重用、扩展类的功能并支持多态性。理解继承的类型、重写与重载、多重继承及其相关问题,对于掌握C++面向对象编程至关重要。希望本文能为您的C++学习和开发提供实用的指导。
58 16