概述:
- 算法主要是由头文件
组成。 是所有STL头文件中最大的一个,范围涉及到比较、交换、查找、遍历操作、复制、修改等等 体积很小,只包括几个在序列上面进行简单数学运算的模板函数 定义了一些模板类,用以声明函数对象。
常用遍历算法
for_each
作用:遍历容器
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
void print1(int val)
{
cout << val << " ";
}
class print2
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
void test()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
//普通函数传入函数名
for_each(v.begin(), v.end(), print1);
cout << endl;
//仿函数传入对象
for_each(v.begin(), v.end(), print2());
cout << endl;
}
int main()
{
test();
}transform
功能:搬运容器到另一个容器中
函数原型:transform(iterator beg1,iterator end1, iterator beg2,_func);
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
class Transform
{
public:
int operator()(int v)
{
return v + 10;
}
};
void test()
{
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(vtarget.begin(), vtarget.end(), [](int val) {cout << val << " "; });
}
int main()
{
test();
}和直接复制不同,transform我们可以在搬运的同时对数据进行一些处理。
常用查找算法
find
函数原型:find(iterator beg, iterator end, value);
按值查找元素,找到了返回迭代器位置,找不到返回结束迭代器位置。
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
#include<ctime>
#include<string>
class Person
{
public:
Person(string name, int age)
{
this->name = name;
this->age = age;
}
string name; int age;
bool operator==(const Person& p)
{
if (this->name == p.name && this->age == p.age)
{
return true;
}
return false;
}
};
void test1()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(rand() % 10);
}
auto it = find(v.begin(), v.end(), 6);
if (it == v.end())
{
cout << "未找到该元素" << endl;
}
else {
cout << "找到了这个元素,为:" << *it << endl;
}
}
void test2()
{
vector<Person>v;
Person p1("aaa", 10);
Person p2("bbb", 12);
Person p3("ccc", 16);
Person p4("ddd", 20);
Person p5("bbb", 12);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
auto it = find(v.begin(), v.end(), p5);
if (it == v.end())
{
cout << "没有找到" << endl;
}
else {
cout << "找到了,姓名为" << it->name << " 年龄:" << it->age << endl;
}
}
int main()
{
srand((unsigned int)time(NULL));
test1();
test2();
}find返回的是一个迭代器,对于自定义数据类型,需要重载一下==号,否则find底层代码会不知道怎么比较,就会报错。
find_if
函数原型:find_if(iterator beg, iterator end,_Pred);
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
#include<ctime>
#include<string>
class Person
{
public:
string name;
int age;
Person(string name, int age)
{
this->name = name;
this->age = age;
}
};
class Greater50
{
public:
bool operator()(int val)
{
return val > 50;
}
};
void test1()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(rand() % 100);
}
auto it = find_if(v.begin(), v.end(), Greater50());
if (it == v.end())
{
cout << "没有找到" << endl;
}
else {
cout << "找到了,为:" << *it << endl;
}
}
void test2()
{
vector<Person>v;
Person p1("tom", 18);
Person p2("jerry", 24);
Person p3("ali", 13);
Person p4("annie", 15);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
auto it = find_if(v.begin(), v.end(), [](const Person& p) {return p.age > 20; });
if (it == v.end())
{
cout << "没有找到" << endl;
}
else {
cout << "找到了。姓名为:" << it->name << " 年龄为:" << it->age << endl;
}
}
int main()
{
srand((unsigned int)time(NULL));
test1();
test2();
}当我们需要按条件查找的时候就可以选择find_if函数了。
adjacent_find
函数原型:adjacent_find(iterator beg, iterator end);
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
void test()
{
vector<int>v;
v.push_back(0);
v.push_back(2);
v.push_back(0);
v.push_back(3);
v.push_back(1);
v.push_back(4);
v.push_back(3);
v.push_back(3);
auto pos = adjacent_find(v.begin(), v.end());
if (pos == v.end())
{
cout << "未找到相邻重复元素" << endl;
}
else {
cout << "找到了相邻重复元素:" << *pos << endl;
}
}
int main()
{
test();
}函数返回的是相邻元素里一个元素的迭代器。
binary_search
功能描述:查找指定元素是否存在
函数原型:bool binary_search(iterator beg, iterator end, value);
查找指定的元素,查到返回true否则false
注意:在无序序列中不可用
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
void test()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
bool ret = binary_search(v.begin(), v.end(), 9);
if (ret)
{
cout << "找到了元素" << endl;
}
else {
cout << "没有找到元素" << endl;
}
}
int main()
{
test();
}二分查找效率很高,但是必须要在有序序列里才可以正确查找,否则可能是对的,也可能是错的。
count
功能描述:统计元素个数
函数原型:
count(iterator beg, iterator end, value);
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
#include<ctime>
void test1()
{
vector<int>v;
for (int i = 0; i < 100; i++)
{
v.push_back(rand() % 30);
}
int num = count(v.begin(), v.end(), 23);
cout << "23的数量为:" << num << endl;
}
class Person {
public:
string name;
int age;
Person(string name, int age)
{
this->name = name;
this->age = age;
}
bool operator==(const Person& p)
{
return this->age == p.age;
}
};
void test2()
{
vector<Person>v;
Person p1("tom", 35);
Person p2("jerry", 28);
Person p3("ali", 35);
Person p4("anno", 16);
Person p5("jim", 45);
Person p6("jan", 35);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
int num = count(v.begin(), v.end(), p6);
cout << "和jan年龄相同的人有:" << num << "个" << endl;
}
int main()
{
srand((unsigned int)time(NULL));
test1();
test2();
}count返回的是一个int型的变量,代表有多少个,对于自定义数据类型,我们需要重载==号,否则count底层的代码会不知道怎么比较,就会报错。
count_if
功能描述:按条件统计元素个数
函数原型:count_if(iterator beg, iterator end,_Pred);
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
#include<ctime>
void test1()
{
vector<int>v;
for (int i = 0; i < 100; i++)
{
v.push_back(rand() % 30);
}
int num = count_if(v.begin(), v.end(), [](int val) {return val > 15; });
cout << "大于15的数量为:" << num << endl;
}
class Person {
public:
string name;
int age;
Person(string name, int age)
{
this->name = name;
this->age = age;
}
bool operator==(const Person& p)
{
return this->age == p.age;
}
};
void test2()
{
vector<Person>v;
Person p1("tom", 25);
Person p2("jerry", 15);
Person p3("ali", 26);
Person p4("anno", 16);
Person p5("jim", 24);
Person p6("jan", 12);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
v.push_back(p6);
int num = count_if(v.begin(), v.end(), [](const Person& p) {return p.age > 20; });
cout << "年龄大于20的人有:" << num << "个" << endl;
}
int main()
{
srand((unsigned int)time(NULL));
test1();
test2();
}常用排序算法
sort
功能描述:对容器内元素进行排序
函数原型:sort(iterator beg, iterator end,_Pred);
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
#include<ctime>
#include<functional>
void test()
{
vector<int>v;
for (int i = 0; i < 30; i++)
{
v.push_back(rand() % 30);
}
for_each(v.begin(), v.end(), [](int val) {cout << val << " "; });
cout << endl;
sort(v.begin(), v.end(), greater<int>());
for_each(v.begin(), v.end(), [](int val) {cout << val << " "; });
}
int main()
{
srand((unsigned int)time(NULL));
test();
}sort是非常常用的排序算法,建议熟练掌握
random_shuffle
功能描述:洗牌指定范围内的元素随机调整次序
函数原型:random_shuffle(iterator beg, iterator end);
在c++17被移除,建议使用shuffle。
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
#include<ctime>
#include<functional>
#include<random>
void test()
{
vector<int>v;
for (int i = 0; i < 30; i++)
{
v.push_back(rand() % 30);
}
for_each(v.begin(), v.end(), [](int val) {cout << val << " "; });
cout << endl;
sort(v.begin(), v.end(), greater<int>());
random_device rd;
mt19937_64 g(rd());
shuffle(v.begin(), v.end(), g);
for_each(v.begin(), v.end(), [](int val) {cout << val << " "; });
}
int main()
{
srand((unsigned int)time(NULL));
test();
}merge
功能描述:两个容器元素合并,并存储到另一容器中
函数原型:merge(iterator beg1,iterator end1,iterator beg2,iterator end2,iterator dest);
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
void test()
{
vector<int>v1;
vector<int>v2;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
v2.push_back(i + 1);
}
vector<int>vtarget;
vtarget.resize(v1.size() + v2.size());
merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vtarget.begin());
for_each(vtarget.begin(), vtarget.end(), [](int val) {cout << val << " "; });
}
int main()
{
test();
}reverse
功能描述:将容器内元素进行反转
函数原型:reverse(iterator beg, iterator end);
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
void test()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
cout << "反转前" << endl;
for_each(v.begin(), v.end(), [](int val) {cout << val << " "; });
reverse(v.begin(), v.end());
cout << endl << "反转后" << endl;
for_each(v.begin(), v.end(), [](int val) {cout << val << " "; });
}
int main()
{
test();
}常用拷贝和替换算法
copy
功能描述:容器内指定范围的元素拷贝到另一容器中
函数原型:copy(iterator beg, iterator end, iterator dest);
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
void test()
{
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());
for_each(v2.begin(), v2.end(), [](int val) {cout << val << " "; });
}
int main()
{
test();
}和之前一些算法是类似的,再使用的时候,目标容器一定要先开辟空间
replace
功能描述:将容器内指定范围的旧元素修改为新元素
函数原型:replace(iterator beg, iterator end, oldvalue, newvalue);
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
void test()
{
vector<int>v1;
v1.push_back(10);
v1.push_back(20);
v1.push_back(30);
v1.push_back(20);
v1.push_back(40);
v1.push_back(50);
cout << "替换前" << endl;
for_each(v1.begin(), v1.end(), [](int val) {cout << val << " "; });
replace(v1.begin(), v1.end(), 20, 66);
cout << endl << "替换后" << endl;
for_each(v1.begin(), v1.end(), [](int val) {cout << val << " "; });
}
int main()
{
test();
}很简单的一个函数,替换区间内满足条件的数据
replace_if
功能描述:将区间内满足条件的元素,替换成指定元素
函数原型:replace_if(iterator beg, iterator end,_pred, newvalue);
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
void test()
{
vector<int>v1;
v1.push_back(10);
v1.push_back(15);
v1.push_back(30);
v1.push_back(20);
v1.push_back(40);
v1.push_back(50);
v1.push_back(5);
v1.push_back(60);
v1.push_back(70);
cout << "替换前" << endl;
for_each(v1.begin(), v1.end(), [](int val) {cout << val << " "; });
replace_if(v1.begin(), v1.end(), [](int val) {return val > 30; }, 66);
cout << endl << "替换后" << endl;
for_each(v1.begin(), v1.end(), [](int val) {cout << val << " "; });
}
int main()
{
test();
}通过谓词,可以按条件筛选一些数据,改变它的值。
swap
功能描述:互换两个容器的元素
函数原型:swap(container c1, container c2);
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
void test()
{
vector<int>v1;
vector<int>v2;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
v2.push_back(i + 10);
}
cout << "交换前" << endl;
for_each(v1.begin(), v1.end(), [](int val) {cout << val << " "; });
cout << endl;
for_each(v2.begin(), v2.end(), [](int val) {cout << val << " "; });
swap(v1, v2);
cout << endl << "交换后" << endl;
for_each(v1.begin(), v1.end(), [](int val) {cout << val << " "; });
cout << endl;
for_each(v2.begin(), v2.end(), [](int val) {cout << val << " "; });
}
int main()
{
test();
}常用算术生成算法
这几个函数需要包含numeric库
accumulate
功能描述:计算区间内容器元素累计总和
函数原型:accumulate(iterator beg, iterator end, value);
#include<iostream>
using namespace std;
#include<vector>
#include<numeric>
void test()
{
vector<int>v1;
for (int i = 0; i <= 100; i++)
{
v1.push_back(i);
}
int total = accumulate(v1.begin(), v1.end(), 0);
cout << "total=" << total << endl;
}
int main()
{
test();
}fill
功能描述:向容器中填充指定的元素
函数原型:fill(iterator beg, iterator end, value);
#include<iostream>
using namespace std;
#include<vector>
#include<numeric>
#include<algorithm>
void test()
{
vector<int>v;
v.resize(10);
for_each(v.begin(), v.end(), [](int val) {cout << val << " "; });
fill(v.begin(), v.end(), 66);
cout << endl;
for_each(v.begin(), v.end(), [](int val) {cout << val << " "; });
}
int main()
{
test();
}这个一般是在容器创建之后有需求改变区间的数据。容器在创建或者resize的时候本身也可以指定填充的值。
常用集合算法
set_intersection
功能描述:求两个容器的交集
函数原型:set_intersection(iterator begl,iterator end1,iterator beg2,iterator end2,iterator dest);
#include<iostream>
using namespace std;
#include<vector>
#include<numeric>
#include<algorithm>
void test()
{
vector<int>v1;
vector<int>v2;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
v2.push_back(i + 5);
}
vector<int>v3;
v3.resize(min(v1.size(), v2.size()));
vector<int>::iterator it = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
for_each(v3.begin(), it, [](int val) {cout << val << " "; });
}
int main()
{
test();
}注意:
- 两个容器必须都是有序序列
- 第三个容器要提前开辟空间,取v1,v2较小的空间大小,因为最坏的情况是一个集合完全包含在另外一个集合内。
- 遍历的时候要把set_intersection函数返回的迭代器作为结束位置。例如例子中我们开辟了10个空间,但是交集就5个元素,如果全部打印就会打印一串0,而set_intersection函数返回的是交集最后一个元素后面的位置。就可以打印出全部的有效元素。
set_union
功能描述:求两个集合的并集
函数原型:set_union(iterator beg1,iterator end1,iterator beg2,iterator end2,iterator dest);
#include<iostream>
using namespace std;
#include<vector>
#include<numeric>
#include<algorithm>
void test()
{
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 it = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), vtarget.begin());
for_each(vtarget.begin(), it, [](int val) {cout << val << " "; });
}
int main()
{
test();
}和上面的函数一样,需要在求并集之前给vtarget指定一个大小,我们要考虑最大的情况,就是两个集合没有重复的元素,所以要开辟的空间是两个集合大小的和。包括打印的结束迭代器是set_union返回的迭代器。和上面同理。
set_difference
功能描述:求两个集合的差集
函数原型:
set_difference(iterator beg1,iterator end1, iterator beg2,iterator end2, iterator dest);
#include<iostream>
using namespace std;
#include<vector>
#include<numeric>
#include<algorithm>
void test()
{
vector<int>v1;
vector<int>v2;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
v2.push_back(i + 5);
}
cout << "v1和v2的差集" << endl;
vector<int>vtarget;
vtarget.resize(max(v1.size(), v2.size()));
vector<int>::iterator it = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), vtarget.begin());
for_each(vtarget.begin(), it, [](int val) {cout << val << " "; });
cout << endl << "v2和v1的差集" << endl;
it = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), vtarget.begin());
for_each(vtarget.begin(), it, [](int val) {cout << val << " "; });
}
int main()
{
test();
}求v1差集简单的说就是用v1集合减去v1和v2的并集。求v2的差集也是同理。
包括打印和前两个函数也没有区别,同样是需要使用函数返回的迭代器作为结束位置。