爱上c++的第十四天:STL-函数对象

简介: 爱上c++的第十四天:STL-函数对象

你的c++学习路上明灯


函数对象


一,概念


1,重载函数调用操作符的类,其对象称为函数对象。


2,函数对象使用重载的()时,行为类似于函数调用,也叫仿函数。


二,本质


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


三,特点


1a7af8adbd1a449e8e041acfc90a9042.png


#include<iostream>
#include<string>
using namespace std;
class MyAdd {
public:
  int operator()(int a, int b) {
    return a + b;
  }
  int count = 0;
};
void test1() {
  MyAdd myadd;
  cout << myadd(29, 30) << endl;
}
class Myprint {
public:
  void operator()(string test) {
    cout << test << endl;
    this->count++;
  }
  int count = 0;
};
void doPrint(Myprint& my,string test) {
  my(test);
}
void test2() {
  Myprint my;
  my("hello world");
  my("hello world");
  my("hello world");
  cout << my.count << endl;
}
void test3() {
  Myprint my;
  doPrint(my, "hello");
}
int main() {
  //test1();
  //test2();
  test3();
  return 0;
}


谓词


一,概念


1,返回布尔类型的仿函数称为谓词


2,如果operator()接收一个参数,就叫做一元谓词


3,如果operator()接收两个参数,就叫做二元谓词


一元谓词


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class func {
public:
  bool operator()(int val) {
    return val > 5;
  }
};
void test1() {
  vector<int>v;
  for (int i = 0; i < 10; i++) {
    v.push_back(i);
  }
  vector<int>::iterator it=find_if(v.begin(), v.end(), func());
  if (it == v.end()) {
    cout << "未找到" << endl;
  }
  else {
    cout << "已找到" << endl;
    cout << *it << endl;
  }
}
int main() {
  test1();
  return 0;
}


二元谓词


#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
class MyCompare {
public:
  bool operator()(int val1,int val2) {
    return val1 > val2;
  }
};
void test1() {
  vector<int>v;
  v.push_back(10);
  v.push_back(30);
  v.push_back(120);
  v.push_back(20);
  sort(v.begin(), v.end());
  for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
    cout << *it << " ";
  }
  cout << endl;
  cout << "------------------" << endl;
  sort(v.begin(), v.end(),MyCompare());
  for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
    cout << *it << " ";
  }
  cout << endl;
}
int main() {
  test1();
  return 0;
}


内建函数对象


一,概念


STL提供的一些函数对象


二,分类


1,算术仿函数


2,关系仿函数


3,逻辑仿函数


三,用法


1,这些仿函数所产生的对象,用法和一般函数完全相同


2,使用内建函数对象,需要引入头文件<functional>


四,实例


1,算术运算符


78f5896b861d4fb6ab073f47352a170a.png


#include<iostream>
#include<functional>
using namespace std;
void test1() {
  //一元运算
  negate<int>n;
  cout << n(20) << endl;
}
void test2() {
  //二元运算
  //默认为相同数据类型的运算
  plus<int>p;
  cout << p(10, 20) << endl;
}
int main() {
  test1();
  test2();
  return 0;
}


2,关系仿函数


7b67b17e9c8649378d67766768790585.png


#include<iostream>
#include<functional>
#include<algorithm>
#include<vector>
using namespace std;
class MyCompare {
public:
  bool operator()(int val1,int val2) {
    return val1 > val2;
  }
};
void test1() {
  vector<int>v;
  v.push_back(10);
  v.push_back(210);
  v.push_back(30);
  v.push_back(50);
  sort(v.begin(), v.end());
  for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
    cout << *it << " ";
  }
  cout << endl;
  //sort(v.begin(), v.end(),MyCompare());
  sort(v.begin(), v.end(), greater<int>());
  for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
    cout << *it << " ";
  }
  cout << endl;
}
int main() {
  test1();
  return 0;
}


3,逻辑仿函数


fc1465d7101d46598efd864b6f024227.png


#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
void test1() {
  vector<bool>v;
  v.push_back(true);
  v.push_back(false);
  v.push_back(true);
  v.push_back(false);
  for (vector<bool>::iterator it = v.begin(); it != v.end(); it++) {
    cout << *it << " ";
  }
  cout << endl;
  //利用逻辑非,取反存储到另一个vector容器中
  vector<bool>v2;
  //一定要指定大小,不然是无法放进去的
  v2.resize(v.size());
  transform(v.begin(), v.end(), v2.begin(), logical_not<bool>());
  for (vector<bool>::iterator it = v2.begin(); it != v2.end(); it++) {
    cout << *it << " ";
  }
  cout << endl;
  cout << endl;
}
int main() {
  test1();
  return 0;
}


目录
相关文章
|
2天前
|
存储 编译器 C语言
c++的学习之路:5、类和对象(1)
c++的学习之路:5、类和对象(1)
17 0
|
2天前
|
C++
c++的学习之路:7、类和对象(3)
c++的学习之路:7、类和对象(3)
19 0
|
1天前
|
存储 搜索推荐 C++
【C++高阶(二)】熟悉STL中的map和set --了解KV模型和pair结构
【C++高阶(二)】熟悉STL中的map和set --了解KV模型和pair结构
|
1天前
|
设计模式 C语言 C++
【C++进阶(六)】STL大法--栈和队列深度剖析&优先级队列&适配器原理
【C++进阶(六)】STL大法--栈和队列深度剖析&优先级队列&适配器原理
|
1天前
|
存储 缓存 编译器
【C++进阶(五)】STL大法--list模拟实现以及list和vector的对比
【C++进阶(五)】STL大法--list模拟实现以及list和vector的对比
|
1天前
|
算法 C++ 容器
【C++进阶(四)】STL大法--list深度剖析&list迭代器问题探讨
【C++进阶(四)】STL大法--list深度剖析&list迭代器问题探讨
|
1天前
|
编译器 C++
【C++进阶(三)】STL大法--vector迭代器失效&深浅拷贝问题剖析
【C++进阶(三)】STL大法--vector迭代器失效&深浅拷贝问题剖析
|
1天前
|
编译器 C++
【C++基础(八)】类和对象(下)--初始化列表,友元,匿名对象
【C++基础(八)】类和对象(下)--初始化列表,友元,匿名对象
|
2天前
|
存储 算法 C语言
c++的学习之路:9、STL简介与string(1)
c++的学习之路:9、STL简介与string(1)
19 0
|
7天前
|
编译器 C++
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(中)——“C++”
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(中)——“C++”

热门文章

最新文章