【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++一分钟之-属性(attributed)与属性语法
【6月更文挑战第28天】C++的属性为代码添加元数据,帮助编译器理解意图。C++11引入属性语法`[[attribute]]`,但支持取决于编译器。常见属性如`nodiscard`提示检查返回值,`maybe_unused`防止未使用警告。问题包括兼容性、过度依赖和误用。使用属性时需谨慎,确保团队共识,适时更新以适应C++新特性。通过示例展示了`nodiscard`和`likely/unlikely`的用法,强调正确使用属性能提升代码质量和性能。
25 13
|
18小时前
|
存储 编译器 C++
【C++】类和对象④(再谈构造函数:初始化列表,隐式类型转换,缺省值
C++中的隐式类型转换在变量赋值和函数调用中常见,如`double`转`int`。取引用时,须用`const`以防修改临时变量,如`const int& b = a;`。类可以有隐式单参构造,使`A aa2 = 1;`合法,但`explicit`关键字可阻止这种转换。C++11起,成员变量可设默认值,如`int _b1 = 1;`。博客探讨构造函数、初始化列表及编译器优化,关注更多C++特性。
|
18小时前
|
编译器 C++
【C++】类和对象④(类的默认成员函数:取地址及const取地址重载 )
本文探讨了C++中类的成员函数,特别是取地址及const取地址操作符重载,通常无需重载,但展示了如何自定义以适应特定需求。接着讨论了构造函数的重要性,尤其是使用初始化列表来高效地初始化类的成员,包括对象成员、引用和const成员。初始化列表确保在对象创建时正确赋值,并遵循特定的执行顺序。
|
19小时前
|
编译器 C++
【C++】类和对象③(类的默认成员函数:赋值运算符重载)
在C++中,运算符重载允许为用户定义的类型扩展运算符功能,但不能创建新运算符如`operator@`。重载的运算符必须至少有一个类类型参数,且不能改变内置类型运算符的含义。`.*::sizeof?`不可重载。赋值运算符`=`通常作为成员函数重载,确保封装性,如`Date`类的`operator==`。赋值运算符应返回引用并检查自我赋值。当未显式重载时,编译器提供默认实现,但这可能不足以处理资源管理。拷贝构造和赋值运算符在对象复制中有不同用途,需根据类需求定制实现。正确实现它们对避免数据错误和内存问题至关重要。接下来将探讨更多操作符重载和默认成员函数。
|
19小时前
|
存储 编译器 C++
【C++】类和对象③(类的默认成员函数:拷贝构造函数)
本文探讨了C++中拷贝构造函数和赋值运算符重载的重要性。拷贝构造函数用于创建与已有对象相同的新对象,尤其在类涉及资源管理时需谨慎处理,以防止浅拷贝导致的问题。默认拷贝构造函数进行字节级复制,可能导致资源重复释放。例子展示了未正确实现拷贝构造函数时可能导致的无限递归。此外,文章提到了拷贝构造函数的常见应用场景,如函数参数、返回值和对象初始化,并指出类对象在赋值或作为函数参数时会隐式调用拷贝构造。
|
19小时前
|
存储 编译器 C语言
【C++】类和对象②(类的默认成员函数:构造函数 | 析构函数)
C++类的六大默认成员函数包括构造函数、析构函数、拷贝构造、赋值运算符、取地址重载及const取址。构造函数用于对象初始化,无返回值,名称与类名相同,可重载。若未定义,编译器提供默认无参构造。析构函数负责对象销毁,名字前加`~`,无参数无返回,自动调用以释放资源。一个类只有一个析构函数。两者确保对象生命周期中正确初始化和清理。
|
1天前
|
程序员 编译器 C++
探索C++语言宝库:解锁基础知识与实用技能(类型变量+条件循环+函数模块+OOP+异常处理)
探索C++语言宝库:解锁基础知识与实用技能(类型变量+条件循环+函数模块+OOP+异常处理)
4 0
|
2天前
|
C++ 容器
C++ STL标准库 《map容器详解》
C++ STL标准库 《map容器详解》
7 0
|
2天前
|
存储 C++ 容器
C++ STL标准库 《map容器详解》
C++ STL标准库 《map容器详解》
8 0
|
2天前
|
缓存 C++
详细解读C++常用库函数C函数库cstdio
详细解读C++常用库函数C函数库cstdio