【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

目录
相关文章
|
4月前
|
人工智能 机器人 编译器
c++模板初阶----函数模板与类模板
class 类模板名private://类内成员声明class Apublic:A(T val):a(val){}private:T a;return 0;运行结果:注意:类模板中的成员函数若是放在类外定义时,需要加模板参数列表。return 0;
93 0
|
7月前
|
安全 C++
【c++】继承(继承的定义格式、赋值兼容转换、多继承、派生类默认成员函数规则、继承与友元、继承与静态成员)
本文深入探讨了C++中的继承机制,作为面向对象编程(OOP)的核心特性之一。继承通过允许派生类扩展基类的属性和方法,极大促进了代码复用,增强了代码的可维护性和可扩展性。文章详细介绍了继承的基本概念、定义格式、继承方式(public、protected、private)、赋值兼容转换、作用域问题、默认成员函数规则、继承与友元、静态成员、多继承及菱形继承问题,并对比了继承与组合的优缺点。最后总结指出,虽然继承提高了代码灵活性和复用率,但也带来了耦合度高的问题,建议在“has-a”和“is-a”关系同时存在时优先使用组合。
354 6
|
8月前
|
编译器 C语言 C++
☺初识c++(语法篇)☺
☺初识c++(语法篇)☺
|
9月前
|
C++ 开发者
C++学习之继承
通过继承,C++可以实现代码重用、扩展类的功能并支持多态性。理解继承的类型、重写与重载、多重继承及其相关问题,对于掌握C++面向对象编程至关重要。希望本文能为您的C++学习和开发提供实用的指导。
136 16
|
10月前
|
算法 网络安全 区块链
2023/11/10学习记录-C/C++对称分组加密DES
本文介绍了对称分组加密的常见算法(如DES、3DES、AES和国密SM4)及其应用场景,包括文件和视频加密、比特币私钥加密、消息和配置项加密及SSL通信加密。文章还详细展示了如何使用异或实现一个简易的对称加密算法,并通过示例代码演示了DES算法在ECB和CBC模式下的加密和解密过程,以及如何封装DES实现CBC和ECB的PKCS7Padding分块填充。
193 4
2023/11/10学习记录-C/C++对称分组加密DES
|
12月前
|
编译器 C语言 C++
配置C++的学习环境
【10月更文挑战第18天】如果想要学习C++语言,那就需要配置必要的环境和相关的软件,才可以帮助自己更好的掌握语法知识。 一、本地环境设置 如果您想要设置 C++ 语言环境,您需要确保电脑上有以下两款可用的软件,文本编辑器和 C++ 编译器。 二、文本编辑器 通过编辑器创建的文件通常称为源文件,源文件包含程序源代码。 C++ 程序的源文件通常使用扩展名 .cpp、.cp 或 .c。 在开始编程之前,请确保您有一个文本编辑器,且有足够的经验来编写一个计算机程序,然后把它保存在一个文件中,编译并执行它。 Visual Studio Code:虽然它是一个通用的文本编辑器,但它有很多插
364 6
|
8月前
|
编译器 C++ 开发者
【C++篇】深度解析类与对象(下)
在上一篇博客中,我们学习了C++的基础类与对象概念,包括类的定义、对象的使用和构造函数的作用。在这一篇,我们将深入探讨C++类的一些重要特性,如构造函数的高级用法、类型转换、static成员、友元、内部类、匿名对象,以及对象拷贝优化等。这些内容可以帮助你更好地理解和应用面向对象编程的核心理念,提升代码的健壮性、灵活性和可维护性。
|
4月前
|
存储 编译器 程序员
c++的类(附含explicit关键字,友元,内部类)
本文介绍了C++中类的核心概念与用法,涵盖封装、继承、多态三大特性。重点讲解了类的定义(`class`与`struct`)、访问限定符(`private`、`public`、`protected`)、类的作用域及成员函数的声明与定义分离。同时深入探讨了类的大小计算、`this`指针、默认成员函数(构造函数、析构函数、拷贝构造、赋值重载)以及运算符重载等内容。 文章还详细分析了`explicit`关键字的作用、静态成员(变量与函数)、友元(友元函数与友元类)的概念及其使用场景,并简要介绍了内部类的特性。
169 0
|
6月前
|
编译器 C++ 容器
【c++11】c++11新特性(上)(列表初始化、右值引用和移动语义、类的新默认成员函数、lambda表达式)
C++11为C++带来了革命性变化,引入了列表初始化、右值引用、移动语义、类的新默认成员函数和lambda表达式等特性。列表初始化统一了对象初始化方式,initializer_list简化了容器多元素初始化;右值引用和移动语义优化了资源管理,减少拷贝开销;类新增移动构造和移动赋值函数提升性能;lambda表达式提供匿名函数对象,增强代码简洁性和灵活性。这些特性共同推动了现代C++编程的发展,提升了开发效率与程序性能。
180 12
|
7月前
|
设计模式 安全 C++
【C++进阶】特殊类设计 && 单例模式
通过对特殊类设计和单例模式的深入探讨,我们可以更好地设计和实现复杂的C++程序。特殊类设计提高了代码的安全性和可维护性,而单例模式则确保类的唯一实例性和全局访问性。理解并掌握这些高级设计技巧,对于提升C++编程水平至关重要。
130 16