STL常用算法
概述:
○ 算法主要由头文件<algorithm><functional><numeric>
组成;
○ algorithm是所有STL头文件中最大的一个,范围涉及到比较、交换、查找、遍历操作、赋值、修改等;
○ functional定义了一些模板类,用以声明函数对象;
○ numeric体积小,只包括几个在序列上面进行简单数学运算的模板函数;
常用遍历算法
- 算法简介:
○for_each()
; //遍历容器
○transform()
; //搬运容器到另一个容器中
for_each算法
- 函数原型:
for_each(iterator.begin(),iterator.end(),_func());
○ iterator.begin() //起始迭代器
○ iterator.end() //结束迭代器
○ _func() //函数或者函数对象
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
//普通函数
void printTest01(int data) {
cout << data << ' ';
}
//仿函数
class printTest02 {
public:
void operator()(int data) {
cout << data << ' ';
}
};
void test01() {
vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back(i + 1);
}
cout << "普通函数:" << endl;
for_each(v.begin(), v.end(), printTest01); //提供函数名
cout << endl;
}
void test02() {
vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back(i + 1);
}
cout << "仿函数:" << endl;
for_each(v.begin(), v.end(), printTest02()); //注意加括号
}
int main() {
test01();
test02();
return 0;
}
/*
普通函数:
1 2 3 4 5 6 7 8 9 10
仿函数:
1 2 3 4 5 6 7 8 9 10
*/
transform算法
- 函数原型:
transform(iterator.begin1,iterator.end,iterator.begin2,_func);
○ iterator.begin1() //源容器起始迭代器
○ iterator.end() //源容器结束迭代器
○ iterator.begin1() //目标容器起始迭代器
○ _func() //函数或者函数对象
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class testTransform01 {
public:
int operator()(int value) {
return value;
}
};
void printTest01(int data) {
cout << data << ' ';
}
void test01() {
vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back(i + 1);
}
vector<int> vTarget;
vTarget.resize(v.size()); //此行代码非常重要,因为目标容器本身没有空间,需要提提前预留空间
transform(v.begin(), v.end(), vTarget.begin(),testTransform01());
for_each(vTarget.begin(), vTarget.end(), printTest01);
}
int main() {
test01();
return 0;
}
/*
1 2 3 4 5 6 7 8 9 10
*/
常用的查找算法
- 简介:
○find();
//查找
○find_if();
//按条件查找
○adjacent_find();
//查找相邻重复元素
○binary_search();
//二分查找
○count();
//计数
○count_if();
//按条件计数
find算法
- 功能简介:查找指定元素,找到返回元素迭代器位置,否则返回结束迭代器位置;
- 函数原型:
find(beginID,endID,value);
- 注意:要用迭代器接收返回值;
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void test01() {
vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back(i - 1);
}
vector<int>::iterator it = find(v.begin(), v.end(), 8);
if (it != v.end()) {
cout << "找到元素了"<< endl;
}
else {
cout << "没有找到" << endl;
}
}
int main() {
test01();
return 0;
}
/*
找到元素了
*/
find_if算法
- 函数原型:
find_if(beginID,endID,_Pred);
○ _Pred – 函数或者谓词
adjacent_find算法
- 功能:返回相邻元素第一个位置的迭代器;
- 函数原型:
adjacent_find(beginID,endID);
binary_search算法
- 功能描述:查找指定元素是否存在
- 函数原型:
bool binary_search(beginID,endID,value);
count算法
- 函数原型:
count(beginID,endID,value);
count_if算法
- 函数原型:
count_if(beginID,endID,_Prad);
常用排序算法
sort算法
- 函数原型:
sort(beginID,endID,_Pred);
- 默认从小到大排序
random_shuffle算法
- 功能:洗牌,将指定范围内的元素随机调整次序;
- 函数原型:random_shuffle(beginID,endID);
- 使用时需要加入随机数种子才真正的随机
○ 头文件:<ctime>
○srand((unsigned int)time(NULL));
merge算法
- 功能描述:将两个容器的元素合并,并存储到另一个容器中;
- 函数原型:
merge(beginID1,endID1,beginID2,endID2,dest)
//dest目标容器的开始迭代器
- 注意:
○ 此处注意预留空间;
○ 两个源容器需要是有序序列;
○ 并且两者的顺序是一致的,要么递增,要么递减;
reverse算法
- 功能:将容器内的元素进行反转;
- 函数原型:
reverse(beginID,endID);
常用拷贝和替换算法
copy算法
- 函数功能:将容器中指定范围内的元素拷贝到另一个容器中;
- 函数原型:
copy(beginID,endID,Dest);
replace算法
- 功能简介:将容器内指定范围内的元素修改为新元素;
- 函数原型:
replace(beginID,endID,oldvalue,newvalue);
replace_if算法
- 功能简介:将容器内满足条件的元素修改为指定元素;
- 函数原型:
replace_if(beginID,endID,_Pred,newvalue);
//_Pred – 谓词,判断条件
swap函数
- 功能描述:互换两个容器的元素;
- 函数原型:
swap(container1,container2);
- 注意:两个容器的类型要一致;
常用算术生成算法
accumulate算法
- 功能描述:计算区间内,容器元素累计总和;
- 头文件:
- 函数原型:
accumulate(beginID,endID,value);
○ value – 起始值累加值,将容器指定区间值累加后会加上这个值;
fill算法
- 函数功能:将指定区间内的填充元素;
- 头文件:
- 函数原型:
fill(beginID,endID,value);
常用的集合算法
set_intersection函数
- 函数功能:取交集;
- 函数原型:
set_intersection(beg1,end1,beg2,end2,dest);
- 注意:两个集合必须是有序集合;
- 通常dest容器的空间用min方法获取两个容器中size的较小值;
set_union函数
- 函数功能:取并集;
- 函数原型:
set_union(beg1,end1,beg2,end2,dest);
- 注意:两个集合必须是有序集合;
set_difference函数
- 函数功能:取差集;
- 函数原型:
set_difference(beg1,end1,beg2,end2,dest);
- 注意:两个集合必须是有序集合;