1、STL-函数对象基本概念
重载函数调用操作符的类,其对象常称为函数对象(function object),即它们是行为类似函数的对象,也叫仿函数(functor),其实就是重载“()”操作符,使得类对象可以像函数那样调用。函数对象是一个类,不是一个函数。
函数对象的特点如下:
- 函数对象在使用时,可以像普通函数那样调用, 可以有参数,可以有返回值
- 函数对象超出普通函数的概念,函数对象可以有自己的状态
- 函数对象可以作为参数传递
示例代码如下所示:
#include <iostream>
#include <string>
#include <map>
using namespace std;
//1、普通函数调用
class MyAdd
{
public:
int operator()(int v1, int v2)
{
return v1 + v2;
}
};
void test01()
{
MyAdd myAdd;
cout << myAdd(10, 10) << endl;
}
//2、函数对象可以有自己的状态
class MyPrint
{
public:
MyPrint()
{
count = 0;
}
void operator()(string test)
{
cout << test << endl;
count++; //统计使用次数
}
int count; //内部自己的状态
};
void test02()
{
MyPrint myPrint;
myPrint("hello world");
myPrint("hello world");
myPrint("hello world");
cout << "myPrint调用次数为: " << myPrint.count << endl;
}
//3、函数对象可以作为参数传递
void doPrint(MyPrint &mp, string test)
{
mp(test);
}
void test03()
{
MyPrint myPrint;
doPrint(myPrint, "Hello C++");
}
int main()
{
cout << "普通函数调用" << endl;
test01();
cout << "函数自身统计" << endl;
test02();
cout << "函数本身传参" << endl;
test03();
system("pause");
return 0;
}
运行结果如下所示:
2、谓词的概念和使用
谓词的基本概念:
- 返回bool类型的仿函数称为谓词
- 如果operator()接受一个参数,那么叫做一元谓词
- 如果operator()接受两个参数,那么叫做二元谓词
下面是一元谓词的基本使用:(参数只有一个)
void test01()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());
if (it == v.end()) {
cout << "没找到!" << endl;
}
else {
cout << "找到:" << *it << endl;
}
}
运行结果如下所示:
下面是二元谓词的基本使用,示例代码如下所示:
class MyCompare
{
public:
bool operator()(int num1, int num2)
{
return num1 > num2;
}
};
void test01()
{
vector<int> v;
v.push_back(10);
v.push_back(40);
v.push_back(20);
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;
cout << "----------------------------" << endl;
//使用函数对象改变算法策略,排序从大到小
sort(v.begin(), v.end(), MyCompare());
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
运行结果如下所示
3、内建函数对象
STL内建了一些函数对象,具体包括:
- 算术仿函数
- 关系仿函数
- 逻辑仿函数
使用这些仿函数所产生的对象,用法和一般函数完全相同,但是需要注意就是使用内建函数对象,需要引入头文件 #include<functional>
下面分别说明一下相关的仿函数:
算术仿函数:(主要是一些四则运算相关的函数)
template T plus | 加法仿函数 |
---|---|
template T minus | 减法仿函数 |
template T multiplies | 乘法仿函数 |
template T divides | 除法仿函数 |
template T modulus | 取模仿函数 |
template T negate | 取反仿函数 |
关系仿函数:(比较相关逻辑的实现)
template bool equal_to | //等于 |
---|---|
template bool not_equal_to | 不等于 |
template bool greater | 大于 |
template bool greater_equal | 大于等于 |
template bool less | 小于 |
template bool less_equal | 小于等于 |
逻辑仿函数:(逻辑运算的实现)
template bool logical_and | 逻辑与 |
---|---|
template bool logical_or | 逻辑或 |
template bool logical_not | 逻辑非 |
4、常用算法
算法主要是由头文件 #include<algorithm>
,#include<functional>
,#include<numeric>
组成,下面是对他们的分别说明:
#include<algorithm>
是所有STL头文件中最大的一个,范围涉及到比较、 交换、查找、遍历操作、复制、修改等等。
#include<numeric>
体积很小,只包括几个在序列上面进行简单数学运算的模板函数。
#include<functional>
定义了一些模板类,用以声明函数对象。
下面是一些常见的算法说明:
遍历算法:
相关函数如下所示:
for_each |
遍历容器 |
---|---|
transform |
搬运容器到另一个容器中 |
遍历示例代码如下所示:
void print01(int val)
{
cout << val << " ";
}
void test01()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
//遍历算法
for_each(v.begin(), v.end(), print01);
cout << endl;
}
运行结果如下所示:
查找算法:
常见的查找算法及说明如下所示:
find |
查找元素 |
---|---|
find_if |
按条件查找元素 |
adjacent_find |
查找相邻重复元素 |
binary_search |
二分查找法 |
count |
统计元素个数 |
count_if |
按条件统计元素个数 |
排序算法:
常见的排序算法及说明如下所示:
sort |
对容器内元素进行排序 |
---|---|
random_shuffle |
洗牌 指定范围内的元素随机调整次序 |
merge |
容器元素合并,并存储到另一容器中 |
reverse |
反转指定范围的元素 |
常用拷贝和替换算法:
copy |
容器内指定范围的元素拷贝到另一容器中 |
---|---|
replace |
将容器内指定范围的旧元素修改为新元素 |
replace_if |
容器内指定范围满足条件的元素替换为新元素 |
swap |
互换两个容器的元素 |