关于我们

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻公共列表

STL-函数对象(仿函数)

发布时间:2023-06-30 16:00:48

概念:

  • 重载函数调用操作符的类,其对象被称为函数对象
  • 函数对象使用重载的()时,行为类似函数调用,也叫仿函数

本质:

  • 函数对象(仿函数)是一个类,不是一个函数

函数对象使用:

  • 函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
  • 函数对象超出普通函数的概念,函数对象可以有自己的状态
  • 函数对象可以作为参数传递
#include#includeusing namespace std; //函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值 class myAdd { public:  int operator()(int v1, int v2)  {  return v1 + v2;  } }; void test1() {  myAdd a1;  cout << a1(1, 2) << endl; } //函数对象超出普通函数的概念,函数对象可以有自己的状态 class myPrint { public:  myPrint()  {  this->count = 0;  }  void operator()(string str)  {  cout << str << endl;  count++;  }  int count; }; void test2() {  myPrint p1;  p1("A");  p1("A");  p1("A");  cout << p1.count << endl; } //函数对象可以作为参数传递 void doPrint(myPrint& mp, string str) {  mp(str); } void test3() {  myPrint mp;  doPrint(mp, "hello"); } int main() {  test1();  test2();  test3();  return 0; }

   


谓词

  • 返回值是bool类型的仿函数被称为谓词
  • 如果operator()接受一个参数,那么叫做一元谓词
  • 如果operator()接受两个参数,那么叫做二元谓词

一元谓词举例

#include#include#includeusing namespace std; class GreaterFive { public:  bool operator()(int val)  {  return val > 5;  } }; int main() {  vectorv;  for (int i = 0; i < 10; i++)  {  v.push_back(i);  }  //查找容器中大于5的元素  //GreaterFive() 匿名函数对象  vector::iterator it=find_if(v.begin(), v.end(), GreaterFive());  cout << *it << endl;  return 0; }

   


二元谓词举例

#include#include#includeusing namespace std; class myCompare { public:  bool operator()(int v1, int v2)  {  return v1 > v2;  } }; int main() {  vectorv;  v.push_back(10);  v.push_back(30);  v.push_back(20);  v.push_back(40);  //默认sort升序排序  sort(v.begin(), v.end());  for (vector::iterator it = v.begin(); it != v.end(); it++)  {  cout << *it << " ";  }  cout << endl;  sort(v.begin(), v.end(), myCompare());  for (vector::iterator it = v.begin(); it != v.end(); it++)  {  cout << *it << " ";  }  return 0; }

   


内建函数对象

STL内建了一些函数对象:

  • 算术仿函数
  • 关系仿函数
  • 逻辑仿函数

用法:

  • 这些仿函数所产生的对象,用法和一般函数完全相同
  • 使用内建仿函数,需要引入头文件

算术仿函数

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

  • templateT puls//加法
  • templateT minus//减法
  • templateT multiplies//乘法
  • templateT modulus//除法
  • templateT negate//取反
#include#includeusing namespace std; int main() {  //取反  negaten;  cout << n(50) << endl;//-50  cout << n(0) << endl;//0  //加法 只填一个模板参数,默认相同类型  plusp;  cout << p(10, 20) << endl;  return 0; }

   


关系仿函数

  • templatebool equal_to//等于
  • templatebool not_equal_to//不等于
  • templatebool greater//大于
  • templatebool greater_equal//大于等于
  • templatebool less//小于
  • templatebool less_equal//小于等于

sort默认的排序规则是从小到大,底层默认使用了less

#include#include#include#includeusing namespace std; int main() {  vectorv;  v.push_back(10);  v.push_back(30);  v.push_back(40);  v.push_back(20);  for (vector::iterator it = v.begin(); it != v.end(); it++)  {  cout << *it << endl;  }  sort(v.begin(), v.end(), less());  for (vector::iterator it = v.begin(); it != v.end(); it++)  {  cout << *it << endl;  }  return 0; }

   


逻辑仿函数

  • templatebool logical_and逻辑与
  • templatebool logical_or逻辑或
  • templatebool logical_not逻辑非
#include#include#include#includeusing namespace std; int main() {  vectorv;  v.push_back(true);  v.push_back(false);  v.push_back(true);  v.push_back(false);  for (vector::iterator it = v.begin(); it != v.end(); it++)  {  cout << *it << " ";  }  cout << endl;  //将容器v搬运到容器v1中,并执行取反操作  vectorv2;  v2.resize(v.size());  //注意没有 v2.end()  transform(v.begin(), v.end(), v2.begin(), logical_not());  for (vector::iterator it = v2.begin(); it != v2.end(); it++)  {  cout << *it << " ";  }  return 0; }

/template/Home/leiyu/PC/Static