STL常用算法
概述
- 算法主要是由头文件 <algorithm> <functional> <numeric>组成
- <algorithm>是所有STL头文件中最大的一个,范围涉及到比较、交换、查找、遍历操作、复制、修改等等
- <numeric>体积很小,只包括几个在序列上面进行简单数学运算的模板函数
- <functional>定义了一些模板类,用以声明函数对象
常用遍历算法
- for_each // 遍历容器
- transform //搬运容器到另一个容器中
for_each
功能描述:
实现遍历容器
函数原型
for_each(iterator beg,iterator end,_func);
//beg 开始迭代器
//end 结束迭代器
//_func 函数或函数对象
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//遍历算法for_each
//普通函数
void print01(int val) {
cout << val << " ";
}
//仿函数
class print02 {
public:
void operator()(int val) {
cout << val << " ";
}
};
void test01() {
vector<int> v;
for (int i = 0;i < 10;i++) {
v.push_back(i);
}
//1.
for_each(v.begin(), v.end(), print01);
cout << endl;
//2.
for_each(v.begin(), v.end(), print02());
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
transform
功能描述:
搬运容器到另一个容器
函数原型
transform(iterator beg1,iterator end1,iterator beg2,_func);
//beg1源容器开始迭代器
//end1 源容器结束迭代器
//beg2 目标容器开始迭代器
//_func 函数或者函数对象
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//transform
class Transform1 {
public:
int operator()(int v) {
return v;
}
};
class print01 {
public:
void operator()(int val) {
cout << val << " ";
}
};
void test01() {
vector<int> v;
for (int i = 0;i < 10;i++) {
v.push_back(i*10);
}
vector<int> v1;//目标容器
v1.resize(v.size());//设置目标容器大小 目标容器需要提前开辟空间
transform(v.begin(), v.end(), v1.begin(),Transform1() );
for_each(v1.begin(), v1.end(), print01());//0 10 20 30 40 50 60 70 80 90
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
注意:搬运容器需要提前给目标容器开辟空间
常用查找算法
- find //查找元素
- find_if //按条件查找元素
- adjacent_find //查找相邻重复元素
- binary_search //二分查找法
- count //统计元素个数
- count_if //按条件统计元素个数
find
功能描述:
查找指定元素,找到返回指定元素的迭代器,找不到返回迭代器end()
函数原型
find(iterator beg,iterator end,value);
//beg 开始迭代器
//end 结束迭代器
//value 查找的元素
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
//find
//查找 内置数据类型
void test01() {
vector<int> v;
for (int i = 0;i < 10;i++) {
v.push_back(i);
}
//查找容器中是否有5这个元素
vector<int>::iterator it = find(v.begin(), v.end(), 5);
if (it != v.end()) {
cout << "容器中有5这个元素" << endl;
}
else {
cout << "容器中没有5这个元素" << endl;
}
}
//查找自定义数据类型
class Person {
public:
Person(string name, int age) {
this->m_Name = name;
this->m_Age = age;
}
//重载== 底层find知道如何对比person数据类型
bool operator==(const Person &p) {
if ((this->m_Name == p.m_Name) && (this->m_Age = p.m_Age)) {
return true;
}
else {
return false;
}
}
string m_Name;
int m_Age;
};
void test02() {
vector<Person> v1;
Person p1("张三", 16);
Person p2("里斯", 14);
Person p3("万物", 156);
Person p4("赵六", 126);
v1.push_back(p1);
v1.push_back(p2);
v1.push_back(p3);
v1.push_back(p4);
vector<Person>::iterator it = find(v1.begin(), v1.end(), p2);
Person pp("张三", 16);
vector<Person>::iterator it = find(v1.begin(), v1.end(), pp);
if (it != v1.end()) {
cout << "容器中有pp这个元素" << endl;
}
else {
cout << "容器中没有p2这个元素" << endl;
}
}
int main() {
test01();
test02();
system("pause");
return 0;
}
find_if
功能描述:
按条件查找元素
函数原型
find_if(iterator beg,iterator end,_Pred);
//按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
//beg 开始迭代器
//end 结束迭代器
//_Pred 函数或谓词(返回bool类型的仿函数)
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
//find_if
//内置数据类型
class GreaterFive {
public:
bool operator()(int val) {
return val > 5;
}
};
void test01() {
vector<int> v;
v.push_back(1);
v.push_back(1);
v.push_back(7);
v.push_back(6);
v.push_back(5);
vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());
if (it != v.end()) {
cout << "找到了大于5的数字" << endl;
cout << "在这个容器中第一个大于5的数字是" << *it << endl;
}
}
//自定义数据类型
class Person {
public:
Person(string name, int age) {
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
class Greater20 {
public:
bool operator()(const Person& p) {
return p.m_Age > 20;
}
};
void test02() {
vector<Person> v;
Person p1("aaa", 15);
Person p2("bbb", 16);
Person p3("ccc", 24);
Person p4("ggg", 21);
Person p5("eee", 17);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
//找年龄大于20的
vector<Person>::iterator it = find_if(v.begin(), v.end(), Greater20());
if (it != v.end()) {
cout << "在容器v中找到年龄大于20的人" << endl;
cout << "第一个的姓名为" << (*it).m_Name << " 年龄为 " << (*it).m_Age << endl;
}
else {
cout << "没有找到年龄大于20的" << endl;
}
}
int main() {
test01();
test02();
system("pause");
return 0;
}
adjacent_find
功能描述:
查找相邻重复元素
函数原型
adjacent_find(iterator beg,iterator end);
//查找相邻重复元素,返回相邻元素的第一个位置的迭代器
//beg 开始迭代器
//end 结束迭代器
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
//adjacent_find
void test01() {
vector<int> v;
v.push_back(5);
v.push_back(4);
v.push_back(1);
v.push_back(5);
v.push_back(5);
v.push_back(6);
v.push_back(8);
v.push_back(8);
vector<int>::iterator it = adjacent_find(v.begin(), v.end());
if (it != v.end()) {
cout << "找到了重复相邻元素" << endl;
cout << "最早的相邻元素为" << *it << endl;
}
else {
cout << "没有重复的相邻元素" << endl;
}
}
int main() {
test01();
system("pause");
return 0;
}
binary_search(二分查找)
功能描述:
查找指定元素是否存在
函数原型
bool binary_search(iterator beg,iterator end,value);
//查找指定的元素,查到返回true,否则 false
//注意:在无序序列中不可用
//beg 开始迭代器
//end 结束迭代器
//value 查找的元素
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
//binary_search
void test01() {
vector<int> v;
for (int i = 0; i < 10;i++) {
v.push_back(i);
}
//v.push_back(1); //原本容器是有序的,增加一个数变成无序,发现查找存在元素的显示出错
bool res = binary_search(v.begin(), v.end(),9);
cout << res << endl;
}
int main() {
test01();
system("pause");
return 0;
}
注意:此方法在无序序列中查找是时好时坏,结果不稳定,故不能用于无序序列
count
功能描述:
统计元素个数
函数原型
count(iterator beg,iterator end,value);
//统计元素出现次数
//beg 开始迭代器
//end 结束迭代器
//value 统计的元素
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
//count
//1.统计内置数据类型
void test01() {
vector<int> v;
v.push_back(1);
v.push_back(1);
v.push_back(4);
v.push_back(1);
v.push_back(6);
v.push_back(7);
int num=count(v.begin(), v.end(), 1);
cout << "容器中1的个数为:" << num << endl;
}
//2.统计自定义数据类型
class Person {
public:
Person(string name, int age) {
this->m_Name = name;
this->m_Age = age;
}
//告诉编译器自定义类型相同的判断
bool operator==(const Person& p) {
if ((this->m_Name == p.m_Name) && (this->m_Age == p.m_Age)) {
return true;
}
else {
return false;
}
}
string m_Name;
int m_Age;
};
void test02() {
vector<Person> v;
Person p1("a", 13);
Person p2("b", 23);
Person p3("s", 33);
Person p4("c", 43);
Person p5("h", 53);
Person p6("a", 13);
Person p7("a", 13);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
v.push_back(p6);
v.push_back(p7);
Person p("a", 13);
int num = count(v.begin(), v.end(), p);
cout << "姓名为a年龄为13的有" << num << "个" << endl;
}
int main() {
test01();
test02();
system("pause");
return 0;
}
count_if
功能描述:
按条件统计元素个数
函数原型
count_if(iterator beg,iterator end,_Pred);
//beg 开始迭代器
//end 结束迭代器
//_Pred 谓词
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
//count_if
//1.统计内置数据类型
class Greater3 {
public:
bool operator()(int val){
return val>3;
}
};
void test01() {
vector<int> v;
v.push_back(1);
v.push_back(1);
v.push_back(4);
v.push_back(1);
v.push_back(6);
v.push_back(7);
int num = count_if(v.begin(), v.end(), Greater3());
cout << "容器中1的个数为:" << num << endl;
}
//2.统计自定义数据类型
class Person {
public:
Person(string name, int age) {
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
class MyCompare30 {
public:
bool operator()(const Person& p) {
if (p.m_Age > 30) {
return true;
}
else {
return false;
}
}
};
void test02() {
vector<Person> v;
Person p1("a", 13);
Person p2("b", 23);
Person p3("s", 33);
Person p4("c", 43);
Person p5("h", 53);
Person p6("a", 13);
Person p7("a", 13);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
v.push_back(p6);
v.push_back(p7);
int num = count_if(v.begin(), v.end(), MyCompare30());
cout << "大于30的人员个数:" << num << endl;
}
int main() {
test01();
test02();
system("pause");
return 0;
}
常用排序算法
算法简介
- sort //对容器内元素进行排序
- random_shuffle //洗牌,指定范围内的元素随机调整次序
- merge //容器元素合并,并存储到另一容器中
- reverse //反转指定范围的元素
sort
功能描述:
对容器内元素进行排序
函数原型
sort(iterator beg,iterator end,_Pred);
//beg 开始迭代器
//end 结束迭代器
//_Pred 谓词
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
#include<functional>
//sort
void myPrint(int val) {
cout << val << " ";
}
void test01() {
vector<int> v;
v.push_back(10);
v.push_back(40);
v.push_back(50);
v.push_back(20);
v.push_back(30);
//默认升序排序
sort(v.begin(), v.end());
for_each(v.begin(), v.end(), myPrint);
cout << endl;
//从大到小排序
sort(v.begin(), v.end(), greater<int>());
for_each(v.begin(), v.end(), myPrint);
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
random_shuffle
功能描述:
洗牌,指定范围内的元素随机调整次序
函数原型
random_suffle(iterator beg,iterator end);
//beg 开始迭代器
//end 结束迭代器
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<ctime>
//random_shuffle
void myPrint(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(), myPrint);
cout << endl;
//打乱顺序的遍历
random_shuffle(v.begin(), v.end());
for_each(v.begin(), v.end(), myPrint);
cout << endl;
}
int main() {
//加入随机数种子,使每次程序启动的随机数都不一样
srand((unsigned int)time(NULL));
test01();
system("pause");
return 0;
}
merge
功能描述:
两个容器元素合并,并存储到另一容器中
函数原型
merge(iterator beg1,iterator end1,iterator beg2,iterator end2,iterator des);
//注意两个容器必须是有序的
//beg1 容器1开始迭代器
//end1 容器1结束迭代器
//beg2 容器2开始迭代器
//end2 容器2结束迭代器
//dest 目标容器开始迭代器
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
//merge
void myPrint(int val) {
cout << val << " ";
}
void test01() {
vector<int> v1;
vector<int> v2;
//有序的容器
for (int i = 0;i < 10;i++) {
v1.push_back(i);
v2.push_back(i*2);
}
vector<int> v3;
//一定要提前给容器分配空间
v3.resize(v1.size() + v2.size());
merge(v1.begin(), v1.end(), v2.begin(), v2.end(),v3.begin());
//打印出来的还是个有序序列
for_each(v3.begin(), v3.end(), myPrint);
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
reverse
功能描述:
将容器内元素进行反转
函数原型
reverse(iterator beg,iterator end);
//beg 开始迭代器
//end 结束迭代器
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
//reverse
void myPrint(int val) {
cout << val << " ";
}
void test01() {
vector<int> v1;
//有序的容器
for (int i = 0;i < 10;i++) {
v1.push_back(i);
}
reverse(v1.begin(), v1.end());
for_each(v1.begin(), v1.end(), myPrint);
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
常用拷贝和替换算法
算法简介
- copy //容器内指定范围的元素拷贝到另一个容器中
- replace //将容器内指定范围的旧元素修改为新元素
- replace_if //容器内指定范围满足条件的元素替换成新元素
- swap //互换两个容器的元素
copy
功能描述:
容器内指定范围的元素拷贝到另一个容器中
函数原型
copy(iterator beg,iterator end,iterator dest);
//beg 开始迭代器
//end 结束迭代器
//dest 目标容器的起始迭代器
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
//copy
void myPrint(int val) {
cout << val << " ";
}
void test01() {
vector<int> v;
//有序的容器
for (int i = 0;i < 10;i++) {
v.push_back(i);
}
vector<int> v1;
//提前开辟空间
v1.resize(v.size());
copy(v.begin(), v.end(), v1.begin());
for_each(v1.begin(), v1.end(), myPrint);
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
replace
功能描述
将容器内指定范围的旧元素改为新元素
函数原型
replace(iterator beg,iterator end,oldvalue,newvalue);
//beg 开始迭代器
//end 结束迭代器
//oldvalue 旧元素
//newvalue 新元素
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
//replace
void myPrint(int val) {
cout << val << " ";
}
void test01() {
vector<int> v1;
for (int i = 0;i < 10;i++) {
v1.push_back(i);
}
cout << "替换前:" << endl;
for_each(v1.begin(), v1.end(), myPrint);
cout << endl;
cout << "替换后:" << endl;
replace(v1.begin(), v1.end(), 2, 100);
for_each(v1.begin(), v1.end(), myPrint);
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
replace会替换满足条件的所有元素
replace_if
功能描述:
将区间内所有满足条件的元素替换成指定元素
函数原型
replace_if(iterator beg,iterator end,_Pred,newvalue);
//beg 开始迭代器
//end 结束迭代器
//_Pred 谓词
//newvalue 替换的新元素
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
//replace_if
void myPrint(int val) {
cout << val << " ";
}
class MyCompare {
public:
bool operator()(int val) {
return val > 5;
}
};
void test01() {
vector<int> v1;
for (int i = 0;i < 10;i++) {
v1.push_back(i);
}
cout << "替换前:" << endl;
for_each(v1.begin(), v1.end(), myPrint);
cout << endl;
cout << "替换后:" << endl;
//将大于5的替换成3
replace_if(v1.begin(), v1.end(), MyCompare(), 3);
for_each(v1.begin(), v1.end(), myPrint);
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
swap
swap(container c1,container c2);
//c1 容器1
//c2 容器2
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
//swap
void myPrint(int val) {
cout << val << " ";
}
void test01() {
vector<int> v1;
for (int i = 0;i < 10;i++) {
v1.push_back(i);
}
vector<int> v2;
for (int i = 0;i < 10;i++) {
v2.push_back(i*10);
}
cout << "替换前:" << endl;
for_each(v1.begin(), v1.end(), myPrint);
cout << endl;
for_each(v2.begin(), v2.end(), myPrint);
cout << endl;
swap(v1, v2);
cout << "替换后:" << endl;
for_each(v1.begin(), v1.end(), myPrint);
cout << endl;
for_each(v2.begin(), v2.end(), myPrint);
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
交换的容器必须是同种类型
常用算数生成算法
注意:
算数生成算法属于小型算法,使用时需包含的头文件为**#include <numeric>**
算法简介
- accumulate //计算容器元素累计总和
- fill //向容器中添加元素
accumulate
功能描述:
计算区间内 容器元素 累计总和
函数原型
accumulate(iterator beg,iterator end,value);
// beg 开始迭代器
//end 结束迭代器
//value 起始值
#include<iostream>
using namespace std;
#include<numeric>
#include<vector>
//accumulate
void test01() {
vector<int> v;
for (int i = 0;i <= 100;i++) {
v.push_back(i);
}
//参数3 起始累加值
int total=accumulate(v.begin(), v.end(),100);
cout << "total=" << total << endl;
}
int main() {
test01();
system("pause");
return 0;
}
fill
功能描述
向容器中填充指定的元素
函数原型
fill(iterator beg,iterator end,value);
//beg 开始迭代器
//end 结束迭代器
//value 填充的值
#include<iostream>
using namespace std;
#include<numeric>
#include<vector>
#include<algorithm>
//fill
void printV(int val) {
cout << val << " ";
}
void test01() {
vector<int> v;
v.resize(8);
fill(v.begin(), v.end(), 8);
for_each(v.begin(), v.end(), printV);
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
常用集合算法
算法简介
- set_intersection //求两个容器的交集
- set_union //求两个容器的并集
- set_difference //求两个容器的差集
set_intersection
功能描述
求两个容器的交集
函数原型
set_intersection(iterator beg1,iterator end1,iterator beg2,iterator end2,iterator dest);
//beg1 容器1的开始迭代器
//end1 容器1的结束迭代器
//beg2 容器2的开始迭代器
//end2 容器2的结束迭代器
//dest 目标容器开始迭代器
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
void printV(int val) {
cout << val << " ";
}
void test01() {
vector<int> v1;
vector<int> v2;
for (int i = 0;i < 10;i++) {
v1.push_back(i);
v2.push_back(i+5);
}
vector<int> vTarget;
//目标容器开辟空间的大小最大为容器中较小的
//vTarget.resize(v1.size() > v2.size() ? v2.size() : v1.size());
vTarget.resize(min(v1.size(), v2.size()));
vector<int>::iterator itEnd=set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
for_each(vTarget.begin(), itEnd,printV);
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
总结:
- 求交集的两个结合必须是有序序列
- 目标容器开辟的空间需要从两个容器中取小值
- set_intersction返回值是交集中最后一个一个元素的位置
set_union
功能描述:
求两个集合的并集
函数原型
set_union(iterator beg1,iterator end1,iterator beg2,iterator end2,iterator dest);
//beg1 容器1的开始迭代器
//end1 容器1的结束迭代器
//beg2 容器2的开始迭代器
//end2 容器2的结束迭代器
//dest 目标容器开始迭代器
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//set_union
void printV(int val) {
cout << val << " ";
}
void test01() {
vector<int> v1;
vector<int> v2;
for (int i = 0;i < 10;i++) {
v1.push_back(i);
v2.push_back(i + 5);
}
vector<int> vTarget;
//开辟空间: 最极端:两个容器没有交集,大小就是两个容器相加
vTarget.resize(v1.size()+v2.size());
vector<int>::iterator itEnd = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
for_each(vTarget.begin(), itEnd, printV);
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
总结:
- 求并集的两个集合必须是有序序列
- 目标容器开辟空间需要两个容器相加
- set_union返回值是并集中最后一个元素的位置
set_difference
功能描述:
求两个集合的差集
函数原型
set_difference(iterator beg1,iterator end1,iterator beg2,iterator end2,iterator dest);
//beg1 容器1的开始迭代器
//end1 容器1的结束迭代器
//beg2 容器2的开始迭代器
//end2 容器2的结束迭代器
//dest 目标容器开始迭代器
注意:
两个集合必须是有序序列
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//set_difference
void printV(int val) {
cout << val << " ";
}
void test01() {
vector<int> v1;
vector<int> v2;
for (int i = 0;i < 10;i++) {
v1.push_back(i);
v2.push_back(i + 5);
}
vector<int> vTarget;
//开辟空间: 最极端:两个容器没有交集,大小就是最大的容器
vTarget.resize(max(v1.size(),v2.size()));
vector<int>::iterator itEnd = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
for_each(vTarget.begin(), itEnd, printV);
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
差集取决于谁在参数前面,如上述代码输出的将是v1和v2的差集