1. 算法头文件
#include<algorithm>
是STL头文件最大的一个,涉及比较、交换、查找、遍历、复制、修改。
#include<numeric>
几个序列上的简单数学运算的模板函数。
#include<functional>
定义了一些模板类
2. 遍历
2.1 for_each
遍历容器
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void Print(int val)//仿函数,定义打印什么东西
{
cout << val;
}
int main()
{
vector<int> v = { 0,3,4,2,1 };
for_each(v.begin(), v.end(), Print);//遍历容器
cout << endl;
system("pause");
return 0;
}
2.2 transform
搬运容器到另一个容器中,此时两个容器都有数据。
int Trans(int val){return val;}//仿函数
vector<int> v1 = { 0,3,4,2,1 };
vector<int> v2;
v2.resize(v1.size());//给v2开辟足够的空间,否则报错
transform(v1.begin(), v1.end(), v2.begin(),Trans);
3. 查找
3.1 find
查找指定元素,返回该元素的迭代器,若无返回end()
vector<int> v1 = { 0,3,4,2,1 };
vector<int>::iterator i = find(v1.begin(), v1.end(), 3);//3是要查找的值
if (i == v1.end())
{
cout << "没有找到!" << endl;
}
else {
cout << *i << endl;
}
3.2 find_if
条件查询
class GtreaterThree
{
public:
bool operator()(int val)
{
return val>3;
}
};
vector<int> v1 = { 0,3,4,2,5 };//有两个比3大,但返回第一个
vector<int>::iterator i = find_if(v1.begin(), v1.end(), GtreaterThree());
if (i == v1.end())
{
cout << "没有找到!" << endl;
}
else {
cout << *i << endl;
}
3.3 adjacet_find
查找相邻元素
vector<int> v1 = { 0,3,4,5,5 };
vector<int>::iterator i = adjacent_find(v1.begin(), v1.end());
3.4 binary_search
二分查找,bool类型,找到返回1,没找到返回0
vector<int> v1 = { 0,3,4,5,5 };
cout << binary_search(v1.begin(), v1.end(), 4) << endl;
3.5 count
统计元素个数
vector<int> v1 = { 0,3,4,5,5 };
cout << count(v1.begin(), v1.end(), 5) << endl;
3.6 count_if
按条件统计元素出现的次数
class GtreaterThree
{
public:
bool operator()(int val)
{
return val>3;
}
};
vector<int> v1 = { 0,3,4,5,5 };
cout << count_if(v1.begin(), v1.end(), GtreaterThree()) << endl;//输出3,有3个数大于3
4. 排序
4.1 sort
默认按从小到大排序
class BigSmall
{
public:
bool operator()(int a,int b)
{
return a>b;
}
};
vector<int> v1 = { 0,3,4,5,5 };
sort(v1.begin(), v1.end());//从小到大排
sort(v1.begin(), v1.end(), BigSmall());//从大到小排
4.2 random_shuffle
洗牌,指定范围内的元素顺序打乱
vector<int> v1 = { 0,3,4,5,5 };
random_shuffle(v1.begin(), v1.end());
上面的代码每次打乱后都是一样的,并不是随机。加入下面两行代码可以实现每次打乱都不一样,真正的随机。
include<ctime>
srand((unsigned int)time(NULL));
4.3 merge
两个容器元素合并,并存储到另外一个容器中
注意:目标容器必须先开辟空间。需要合并的两个容器必须同时升序或者降序。
vector<int> v1 = { 0,3,4,5,5 };
vector<int> v2 = { 0,1,2,3,4 };
vector<int> v3;
v3.resize(v1.size() + v2.size());
merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
4.4 reverse
容器元素顺序反转
vector<int> v1 = { 0,3,4,5,5 };
reverse(v1.begin(), v1.end());//55430
5. 拷贝和替换算法
5.1 copy
将一个容器指定范围内的数据拷贝到另一容器
注意:需要先开辟空间
vector<int> v1 = { 0,3,4,5,5 };
vector<int> v2;
v2.resize(v1.size());
copy(v1.begin(), v1.end(),v2.begin());
5.2 replace
把容器内的某元素替换为另外的元素
vector<int> v1 = { 0,3,4,5,5 };
replace(v1.begin(), v1.end(),5,6);//03466
5.3 replace_if
把容器内的满足条件的元素替换为另外的元素
vector<int> v1 = { 0,3,4,5,5 };
replace_if(v1.begin(), v1.end(), GreaterThree(),6);//03666
5.4 swap
交换两容器的数据,容器大小可不同
vector<int> v1 = { 0,3,4,5,5 };
vector<int> v2;
swap(v1, v2);
6. 算数生成算法
需要加头文件
include<numeric>
6.1 accumulate
计算区间内容器元素累计总和
vector<int> v1 = { 0,3,4,5,5 };
cout << accumulate(v1.begin(), v1.end(), 0) << endl;//0为累加起始值
6.2 fill
用指定的值填充容器,会覆盖之前的内容
vector<int> v1 = { 0,3,4,5,5 };
fill(v1.begin(), v1.end(), 6);//66666
7. 集合算法
返回迭代器
7.1 set_intersection 求两容器交集
7.2 set_union 求两容器并集
两集合必须有序
7.3 set_difference 求两容器差集
两集合必须有序,差集结果与两容器的先后顺序有关。
vector<int> v1 = { 0,3,4,5,5 };
vector<int> v2 = { 1,2,3 };
vector<int> v3,v4,v5,v6;
vector<int>::iterator i3, i4, i5, i6 ;
v3.resize(min(v1.size(), v2.size()));//交集
i3=set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
v4.resize(v1.size()+v2.size());//并集
i4=set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), v4.begin());
v5.resize(v1.size());//差集 v1在前
i5=set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), v5.begin());
v6.resize(v2.size());//差集 v2在前
i6 = set_difference( v2.begin(), v2.end(), v1.begin(), v1.end(), v6.begin());
for_each(v3.begin(), i3, Print);//3
cout << endl;
for_each(v4.begin(), i4, Print);//0123455
cout << endl;
for_each(v5.begin(), i5, Print);//0455
cout << endl;
for_each(v6.begin(), i6, Print);//12
cout << endl;