目录
概述
一、常用遍历算法
(1)for_each
(2)transform
二、常用查找算法
(1)find
(2)find_if
(3)adjacent_find
(4)binary_search
(5)count
(6)count_if
三、常用排序算法
(1)sort
(2)random_shuffle
(3)reverse
(4)merge
四、常用拷贝和替换算法
(1)copy
(2)replace
(3)replace_if
(4)swap
五、常用算数生成算法
(1)accumulate
(2)fill
六、常用集合算法
(1)set_intersection
(2)set_union
(3)set_difference
概述
- 算法主要由头文件<algorithm><functional><numeric>组成
- <algorithm>是所有STL头文件中最大的一个,范围涉及到比较、交换、查找、遍历操作、复制、修改等
- <numeric>体积很小,只包括几个在序列上面简单数学运算的函数模版
- <functional>定义了一下模版类,用以声明函数对象
一、常用遍历算法
1、学习目标:掌握常用的遍历算法
2、算法简介:
- for_each //遍历容器
- transform //搬运容器到另一个容器
3、常用遍历算法
(1)for_each
- 功能描述:实现遍历容器
- 函数原型:
for_each(iterator beg, iterator end, _func);
//beg 开始迭代器
//end 结束迭代器
//_func 函数或者函数对象
- 示例:
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;//普通函数
void print01(int val)
{cout << val << " ";
}//仿函数
class print02
{
public:void operator()(int val){cout << val <<" ";}
};//常用遍历算法 for_each
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;for_each(v.begin(), v.end(), print02());cout << endl;
}int main()
{test01();system("pause");return 0;
}
(2)transform
- 功能描述:搬运容器到另一个容器中
- 函数原型:
transform(iterator beg1, iterator end1, iterator beg2, _func);
//beg1 源容器开始迭代器
//end1 源容器结束迭代器
//beg2 目标容器开始迭代器
//_func 函数或者函数对象
可以在搬运的时候进行逻辑运算,也可以不进行逻辑运算(不进行逻辑运算时return v 就行)
- 示例:
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;//常用的遍历算法 transformclass Transform
{
public:int operator()(int v){return v + 100;}
};class myPrint
{
public:void operator()(int val){cout << val << " ";}};
void test01()
{vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}vector<int> vTarget;//目标容器vTarget.resize(v.size());transform(v.begin(), v.end(), vTarget.begin(), Transform());for_each(v.begin(), v.end(), myPrint());cout << endl;for_each(vTarget.begin(), vTarget.end(), myPrint());cout << endl;
}int main()
{test01();system("pause");return 0;
}
注意:目标容器需要提前开辟空间
vTargrt.resize(v.size());
二、常用查找算法
1.学习目标:掌握常用的查找算法
2.算法介绍:
- find //查找元素是否存在
- find_if //按条件查找元素
- adjacent_find //查找相邻重复元素
- binary_search //二分查找法
- count //统计元素个数
- count_if //按条件统计元素个数
3.算法示例
(1)find
- 功能描述:查找指定元素,找到返回指定元素的迭代器,找不到返回结束迭代器end()
- 函数原型:
find(iterator beg,iterator end,value)
//beg开始迭代器
//end结束迭代器
//value查找的元素
- 示例:查找 内置数据类型 和 自定义数据类型
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>//查找 内置数据类型
void test01()
{vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}//查找容器中 是否有50 这个元素vector<int>::iterator it = find(v.begin(), v.end(), 50);if (it == v.end()){cout << "没有找到" << endl;}else{cout << "找到:" << *it << 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>v;//创建数据Person p1("aaa", 1);Person p2("bbb", 2);Person p3("ccc", 3);Person p4("aaa", 4);//放入容器中v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);Person pp("bbb", 2);vector<Person>::iterator it = find(v.begin(), v.end(), pp);if (it == v.end()){cout << "没有找到" << endl;}else{cout << "找到: 姓名:" << it->m_Name<<" 年龄"<<it->m_Age << endl;;}
}int main()
{test01();system("pause");return 0;
}
总结:利用find可以在容器中找指定元素,返回值是迭代器
(2)find_if
- 功能描述:按照条件查找元素
- 函数原型:
find_if(iterator beg, iterator end, _Pred);
//beg开始迭代器
//end结束迭代器
//_Pred函数或者谓词(返回bool类型的仿函数)
- 示例:
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>//find_if查找 内置数据类型class GreatFive
{
public:bool operator()(int val){return val > 5;}
};
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(), GreatFive());if (it == v.end()){cout << "没有找到" << endl;}else{cout << "找到大于五的数为:" << *it << endl;}
}//find_if查找 自定义数据类型
class Person
{
public:Person(string name, int age){this->m_Name = name;this->m_Age = age;}string m_Name;int m_Age;
};class Great20
{
public:bool operator()(Person &p){return p.m_Age > 20;}
};
void test02()
{vector<Person> v;//创建数据Person p1("aaa", 10);Person p2("bbb", 20);Person p3("ccc", 30);Person p4("ddd", 40);v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);//找年龄大于20的人vector<Person>::iterator it = find_if(v.begin(), v.end(), Great20());if (it == v.end()){cout << "没有找到" << endl;}else{cout << "找到年龄大于20 : 姓名:" << it->m_Name << " 年龄:" << it->m_Age << endl;;}
}int main()
{//test01();test02();system("pause");return 0;}
(3)adjacent_find
- 功能描述:查找相邻元素,返回相邻元素的第一个位置的迭代器
- 函数原型:
adjacent_find(iterator beg, iterator end);
//beg开始迭代器
//end结束迭代器
- 示例:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;void test01()
{vector<int> v;v.push_back(0);v.push_back(2);v.push_back(0);v.push_back(4);v.push_back(1);v.push_back(4);v.push_back(1);v.push_back(3);vector<int>::iterator pos = adjacent_find(v.begin(), v.end());if (pos == v.end()){cout << "未找到相邻元素" << endl;}else{cout << "找到相邻元素: "<<*pos << endl;}
}int main()
{test01();system("pause");return 0;
}
(4)binary_search
- 功能描述:查找指定元素是否存在,返回bool值(查到返回true,否则false)
- 函数原型:
bool binary_search(iterator beg, iterator end, value)
//注意:在无序序列中不可用
//beg开始迭代器
//end结束迭代器
//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);}//查找容器中是否有9 元素//v.push_back(2);//如果加入2后变无序序列,结果未知//注意:容器必须是有序序列bool ret = binary_search(v.begin(), v.end(), 9);if (ret){cout << "找到元素" << endl;}else{cout << "未找到元素" << endl;}}int main()
{test01();system("pause");return 0;
}
(5)count
- 功能描述:统计元素出现次数
- 函数原型:
count(iterator beg, iterator end, value)
//beg开始迭代器
//end结束迭代器
//value统计的元素
- 示例:
#include<iostream>
#include<vector>
using namespace std;
#include<algorithm>//统计 内置数据类型
void test01()
{vector<int>v;v.push_back(10);v.push_back(40);v.push_back(30);v.push_back(40);v.push_back(20);v.push_back(40);int num = count(v.begin(), v.end(), 40);cout << "40的元素个数为: " << num << endl;
}//统计 自定义数据类型
class Person
{
public:Person(string name, int age){this->m_Name = name;this->m_Age = age;}bool operator==(const Person& p)//重载了“==”{if (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", 10);Person p2("b", 20);Person p3("c", 30);Person p4("d", 40);Person p5("d", 30);v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);v.push_back(p5);Person p("e", 50);//查找和p相同30岁的人int num = count(v.begin(), v.end(), p);if (num){cout << "和e同岁的人有:" << num << endl;}else{cout << "无和e同岁的人" << endl;}}int main()
{//test01();test02();system("pause");return 0;
}
(6)count_if
- 功能描述:按照条件统计元素个数
- 函数原型:
coynt_if(iterator beg, iterator end, _Pred)
//beg开始迭代器
//end结束迭代器
//_Pred谓词
- 示例:
#include<iostream>
#include<vector>
using namespace std;
#include<algorithm>//常用查找算法 count_if
//统计 内置数据类型
class Great30
{
public:bool operator()(int val){return val > 30;}
};void test01()
{vector<int> v;v.push_back(10); v.push_back(40);v.push_back(30);v.push_back(40);v.push_back(40);int num = count_if(v.begin(), v.end(), Great30());if (num){cout << "大于30的数有:" << num << "个" << endl;}else{cout << "无大于30的数" << endl;}}//统计 自定义数据类型
class Person
{
public:Person(string name, int age){this->m_Name = name;this->m_Age = age;}string m_Name;int m_Age;
};class AgeGreat20
{
public:bool operator()(const Person &p1){return p1.m_Age > 20;}
};void test02()
{vector<Person>v;Person p1("aaa", 10);Person p2("bbb", 20);Person p3("ccc", 30);Person p4("ddd", 40);Person p5("eee", 30);v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);v.push_back(p5);int num = count_if(v.begin(), v.end(), AgeGreat20());cout << "大于20人员的个数为:" << num << endl;
}int main()
{//test01();test02();system("pause");return 0;
}
三、常用排序算法
常用的排序算法
- sort //对容器内元素进行排序
- random_shuffle //洗牌,指定范围内的元素随机调整次序
- merge //容器元素合并,并存储到另一容器
- reverse //反转指定范围的元素
(1)sort
- 功能描述:对容器内元素进行排序
- 函数原型:
sort(iterator beg, iterator end, _Pred)
//beg开始迭代器
//end结束迭代器
//_Pred谓词,默认从小到大,可以改变排序规则
- 示例:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;//常用排序算法 sort
void myPrint(int val)
{cout << val << " ";
}void test01()
{vector<int> v;v.push_back(10);v.push_back(30);v.push_back(20);v.push_back(50);v.push_back(40);//利用sort进行升序排序sort(v.begin(), v.end());for_each(v.begin(), v.end(),myPrint);cout << endl;//改变 降序排序sort(v.begin(), v.end(), greater<int>());//greater<int>()是内置函数for_each(v.begin(), v.end(), myPrint);cout << endl;
}int main()
{test01();system("pause");return 0;
}
(2)random_shuffle
- 功能描述:洗牌,指定范围内的元素随机调整次序
- 函数原型:
random_shuffle(iterator beg, iterator end)
//beg开始迭代器
//end结束迭代器
- 示例:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#include<ctime>void myPrint(int val)
{cout<<val<<" ";
}
void test01()
{srand((unsigned)time(NULL));//随机数种子vector<int> v;for (int i = 0; i < 10; i++){v.push_back(i);}//利用洗牌算法 打乱顺序random_shuffle(v.begin(), v.end());for_each( v.begin(), v.end(), myPrint);cout << endl;
}int main()
{test01();system("pause");return 0;
}
总结:记得加上随机数种子srand((unsigned)time(NULL)),才能模拟真实的随机
(3)reverse
- 功能描述:将容器内元素进行反转
- 函数原型:
reverse(iterator beg, iterator end);
//beg开始迭代器
//end结束迭代器
- 示例:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;//常用排序算法 reversevoid myPrint(int val)
{cout << val << " ";
}
void test01()
{vector<int>v;v.push_back(10);v.push_back(20);v.push_back(40);v.push_back(30);v.push_back(50);v.push_back(80);cout << "反转前:" << endl;for_each(v.begin(), v.end(), myPrint);cout << endl;cout << "反转后:" << endl;reverse(v.begin(),v.end());for_each(v.begin(), v.end(), myPrint);cout << endl;
}int main()
{test01();system("pause");return 0;
}
(4)merge
- 功能描述: 两个容器元素合并,并存储到另一容器中 两个容器元素必须是有序的,都是同样顺序的,不能一个升序一个降序,而且合并之后的新容器也是有序的
- 函数原型: merge (iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
- 示例:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;void myPrint(int val)
{cout << val << " ";
}void test01()
{//first 和 second 数组中各存有 1 个有序序列int first[] = { 5,10,15,20,25 };int second[] = { 7,17,27,37,47,57 };//用于存储新的有序序列vector<int> myvector(11);//将 [first,first+5) 和 [second,second+6) 合并为 1 个有序序列,并存储到myvector容器中。merge(first, first + 5, second, second + 6, myvector.begin());//输出 myvector 容器中存储的元素for_each(myvector.begin(), myvector.end(), myPrint);cout << endl;
}int main()
{test01();system("pause");return 0;
}
四、常用拷贝和替换算法
常用拷贝和替换算法
- copy() //容器内指定范围的元素拷贝到另一个容器中
- replace() //容器内指定范围的旧元素修改为新元素
- replace_if() //容器内指定范围满足条件的元素替换为新元素
- swap() //互换两个容器元素
(1)copy
- 功能描述:容器内指定范围的元素拷贝到另一个容器中
- 函数原型:
copy(iterator beg, iterator end, iterator dest)
//beg开始迭代器
//end结束迭代器
//dest目标起始迭代器
- 示例:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;//常用的拷贝和替换算法 copy
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;v2.resize(v1.size());copy(v1.begin(), v1.end(), v2.begin());//等价于使用 v2=v1for_each(v2.begin(), v2.end(), myPrint);cout << endl;
}int main()
{test01();system("pause");return 0;
}
总结:在利用copy算法进行拷贝时,目标容器记得提前开辟空间
(2)replace
- 功能描述:容器内指定范围的旧元素修改为新元素
- 函数原型:
replace(iterator beg, iterator end, oldvalue,newvalue)
//beg开始迭代器
//end结束迭代器
//oldvalue旧元素
//newvalue新元素
- 示例:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;//常用的拷贝和替换算法 replace//仿函数
class myPrint
{
public:void operator()(int val){cout << val << " ";}
};void test01()
{vector<int> v;v.push_back(20);v.push_back(30);v.push_back(20);v.push_back(50);v.push_back(10);v.push_back(30); v.push_back(100);v.push_back(30);cout << "替换前:" << endl;for_each(v.begin(), v.end(), myPrint());cout << endl;cout << "替换后:" << endl;//将20替换为2000replace(v.begin(), v.end(), 20, 2000);for_each(v.begin(), v.end(), myPrint());cout << endl;
}int main()
{test01();system("pause");return 0;
}
总结:replace会替换区间内索引满足条件的元素
(3)replace_if
- 功能描述:容器内指定范围满足条件的元素替换为新元素
- 函数原型:
replace(iterator beg, iterator end, _Pred, newvalue)
//beg开始迭代器
//end结束迭代器
//_Pred谓词
//newvalue旧元素
- 示例:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;//常用的拷贝和替换算法 replace_if
class myPrint
{
public:void operator()(int val){cout << val << " ";}
};class great30 //仿函数
{
public:bool operator()(int val){return val >= 30;}
};void test01()
{vector<int> v;v.push_back(20);v.push_back(30);v.push_back(50);v.push_back(50);v.push_back(30);v.push_back(10);v.push_back(10);v.push_back(60);v.push_back(10);v.push_back(30);cout << "替换前:" << endl;for_each(v.begin(), v.end(), myPrint());cout << endl;//将大于等于30 替换为3000replace_if(v.begin(), v.end(), great30(), 3000);cout << "替换后:" << endl;for_each(v.begin(), v.end(), myPrint());cout << endl;}int main()
{test01();system("pause");return 0;
}
(4)swap
- 功能描述:互换两个容器元素
- 函数原型:
swap(container c1,container c2);
//c1容器
//c2容器
- 示例:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;//常用拷贝和替换算法 swap
class myPrint
{
public:void operator()(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(100 + i);}cout << "交换前:" << endl;for_each(v1.begin(), v1.end(), myPrint());cout << endl;for_each(v2.begin(), v2.end(), myPrint());cout << endl;cout << "----------------------------" << endl;cout << "交换后:" << endl;swap(v1, v2);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;
}
总结:swap交换容器时,注意交换的容器要同种类型
五、常用算数生成算法
常用的算数生成算法
- accumulate //计算容器元素累计总和
- fill //向容器中添加元素
注意:算术生成算法属于小型算法,使用时包含头文件为#include<numeric>
(1)accumulate
- 功能描述:计算区间内 容器元素累计和
- 函数原型:
accumulate(iterator beg, iterator end, value)
//beg开始迭代器
//end结束迭代器
//起始的累加值
- 示例:
#include<iostream>
#include<vector>
#include<numeric>
using namespace std;//常用算术生成算法int main()
{vector<int>v;for (int i = 0; i < 101; i++){v.push_back(i);}int total = accumulate(v.begin(), v.end(), 0);//参数3是起始的累加值cout << "total = " << total << endl;system("pause");return 0;
}
(2)fill
- 功能描述:向容器中填充指定的元素
- 函数原型:
fill(iterator beg, iterator end, value)
//beg开始迭代器
//end结束迭代器
//起始的累加值
- 示例:
#include<iostream>
#include<vector>
#include<numeric>
#include<algorithm>
using namespace std;void myPrint(int val)
{cout << val << " ";
}//常用算术生成算法 fill
void test01()
{vector<int> v;v.resize(10);//后期重新填充fill(v.begin(), v.end(), 100);for_each(v.begin(), v.end(), myPrint);cout << endl;
}int main()
{test01();system("pause");return 0;
}
六、常用集合算法
常用的集合算法:
- set_intersection //求两个容器的交集
- set_union //求两个容器的并集
- set_difference //求两个容器的差集
(1)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>
#include<vector>
#include<numeric>
#include<algorithm>
using namespace std;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);//0~9v2.push_back(i+5);//5~14}vector<int> vTarget;//目标容器需要提前开辟空间//最特殊的情况 大容器包含小容器//开辟空间 取小容器的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 , myPrint);cout << endl;}int main()
{test01();system("pause");return 0;
}
总结:
- 求交集的两个集合必须是有序序列
- 目标容器开辟空间需要从两个容器中取较小size
- set_intersection返回值是交集中最后一个元素的位置
(2)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>
#include<vector>
#include<numeric>
#include<algorithm>
using namespace std;class myPrint
{
public:void operator()(int val){cout << val << " ";}
};//常用集合算法 set_union
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;//目标容器要提前开辟空间//最特殊的情况 两个容器没有交集,并集就是两个容器size相加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 , myPrint());cout << endl;
}int main()
{test01();system("pause");return 0;
}
- 求并集的两个集合必须是有序序列
- 目标容器开辟空间需要从两个容器相加
- set_union返回值是并集中最后一个元素的位置
(3)set_difference
- 功能描述:求两个集合的差集
- 函数原型:
set_difference(iterator beg1, iterator end1,iterator beg2, iterator end2, terator dest)
注意:两个集合必须是有序序列
//beg1容器1开始迭代器
//end1容器1结束迭代器
//beg2容器2开始迭代器
//end2容器2结束迭代器
//dest目标容器起始迭代器
- 示例:
#include<iostream>
#include<vector>
#include<numeric>
#include <algorithm>
using namespace std;class myPrint
{
public:void operator()(int val){cout << val << " ";}
};//常用集合算法 set_difference
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;//给目标容器开辟空间//最特殊情况 两个容器没有交集,取两个容器中大的size作为目标容器开辟空间vTarget.resize(max(v1.size(), v2.size()));cout << "v1与v2的差集为:" << endl;vector<int>::iterator itEnd = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());for_each(vTarget.begin(), itEnd, myPrint());cout << endl;cout << "———————————————————————————" << endl;cout << "v2与v1的差集为:" << endl;vector<int>::iterator itEnd1 = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), vTarget.begin());for_each(vTarget.begin(), itEnd1, myPrint());cout << endl;
}int main()
{test01();system("pause");return 0;}
- 求差集的两个集合必须是有序序列
- 目标容器开辟空间需要从两个容器取较大
- set_union返回值是差集中最后一个元素的位置