1. 函数对象/仿函数
函数对象概念
- 重载 函数调用操作符 的类,其对象常称为函数对象
- 函数对象 使用重载的()时,行为类似函数调用,也叫仿函数
- 本质:函数对象(仿函数)是一个类,不是一个函数
函数对象使用
特点:
- 函数对象可以像普通函数那样调用,可以有参数,可以有返回值
- 函数对象超出普通函数的概念,可以有自己的状态
- 函数对象可以作为参数传递
简单示例:
class MyPrint{
public:
MyPrint() {
this->count = 0;
}
int operator()(std::string str) {
std::cout << str << std::endl;
count++;
}
int count;
};
void doPrint(MyPrint & mp,std::string str){
mp(str);
}
// 测试函数对象的使用
void testFuncObj() {
MyPrint myPrint;
doPrint(myPrint,"调用函数");
doPrint(myPrint,"调用函数");
std::cout << "函数调用次数:"<< myPrint.count<< std::endl;
}
int main() {
testFuncObj();
return 0;
}
打印结果:
调用函数
调用函数
函数调用次数:2
2. 谓词
谓词概念
- 返回bool类型的仿函数称为谓词
- 如果operator()接受一个参数,那么叫一元谓词
- 如果operator()接受两个参数,那么叫二元谓词
谓词使用示例
一元谓词:
class Pred{
public:
bool operator()(int num) {
return num>5;
}
};
void testPred1() {
std::vector<int> vector;
for (int i = 0; i < 10; ++i) {
vector.push_back(i);
}
for (std::vector<int>::iterator it = std::find_if(vector.begin(),vector.end(),Pred()); it != vector.end(); it++) {
std::cout << "找到大于5的数字:" << *it <<std::endl;
}
}
int main() {
testPred1();
return 0;
}
输出结果:
找到大于5的数字:6
找到大于5的数字:7
找到大于5的数字:8
找到大于5的数字:9
二元谓词:
// 测试谓词的使用
class Pred{
public:
bool operator()(int num) {
return num>5;
}
bool operator()(int val1, int val2) {
return val1 > val2;
}
};
void testPred2() {
std::vector<int> vector;
vector.push_back(10);
vector.push_back(-2);
vector.push_back(80);
vector.push_back(1);
vector.push_back(104);
vector.push_back(23);
vector.push_back(67);
// std::sort(vector.begin(),vector.end()); // 默认是升序
std::sort(vector.begin(),vector.end(),Pred()); // 通过二元谓词改为降序
for (std::vector<int>::iterator it = vector.begin(); it != vector.end() ; it++) {
std::cout << *it << std::endl;
}
}
int main() {
testPred2(); // 二元谓词
return 0;
}
3. 内建函数对象
STL内建了一些函数对象,这些仿函数所产生的对象,用法和一般函数完全相同,使用内建函数对象需要引入头文件:`#include <functional>` 分类如下:
- 算术仿函数
- 关系仿函数
- 逻辑仿函数
算术仿函数
实现四则运算,其中negate是一元运算,其它都是二元运算
仿函数原型
- template<class T> T plus<T> // 加法仿函数
- template<class T> T minus<T> // 减法仿函数
- template<class T> T multiplies<T> // 乘法仿函数
- template<class T> T divides<T> // 加法仿函数
- template<class T> T modulus<T> // 减法仿函数
- template<class T> T negate<T> // 乘法仿函数
算数仿函数使用示例
void testNegate() {
std::negate<int> n;
std::cout << n(50) << std::endl;
}
void testAdd() {
std::plus<int> n;
std::cout << n(10,20) <<std::endl;
}
int main() {
testNegate();
testAdd();
return 0;
}
运行结果
-50
30
关系仿函数
实现关系对比
仿函数原型
- template<class T> bool equal_to<T> // 等于
- template<class T> bool not_equal_to<T> // 不等于
- template<class T> bool greater<T> // 大于
- template<class T> bool greater_equal<T> // 大于等于
- template<class T> bool less<T> // 小于
- template<class T> bool less_equal<T> // 小于等于
使用示例
void testGreater() {
std::vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(-10);
v.push_back(3);
v.push_back(109);
v.push_back(-66);
// std::sort(v.begin(), v.end()); // 原来的sort是升序排列
std::sort(v.begin(),v.end(),std::greater<int>()); // 降序排列
for (std::vector<int>::iterator it = v.begin(); it != v.end(); ++it) {
std::cout << *it << std::endl;
}
}
int main() {
testGreater(); // 内建函数关系仿函数,大于关系
return 0;
}
输出:
-66
-10
3
10
20
109
逻辑仿函数
实现逻辑运算
函数原型
- template<class T> bool logical_and<T> 逻辑与
- template<class T> bool logical_or<T> 逻辑或
- template<class T> bool logical_not<T> 逻辑非
使用示例
void testLogical() {
std::vector<bool> v;
v.push_back(true);
v.push_back(false);
v.push_back(true);
v.push_back(false);
std::vector<bool> v2;
v2.resize(v.size());
std::transform(v.begin(),v.end(),v2.begin(),std::logical_not<bool>());
for (std::vector<bool>::iterator it = v.begin(); it != v.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
for (std::vector<bool>::iterator it = v2.begin(); it != v2.end(); ++it) {
std::cout << *it << " ";
}
}
int main() {
testLogical(); // 内建函数逻辑仿函数,逻辑非
return 0;
}
输出:
1 0 1 0
0 1 0 1
常用算法
算法主要是有头文件<algorithm> <functional> <numeric> 组成
- <algorithm>:所有STL头文件中最大的一个,范围涉及比较、交换、查找、遍历操作、复制、修改等等
- <functional>:体积很小,只包括几个在序列上面进行简单数学运算的函数模版
- <numeric>:定义了一些模版类,用以声明函数对象
常用遍历算法
遍历容器:for_each
搬运容器到另一个容器中:transform
常用查找算法
- 查找元素:find
- 按条件查找元素:find_if
- 查找相邻重复元素:adjacent_find
- 二分查找法:binary_search
- 统计元素个数:count
- 按条件统计元素个数:count_if