一、算法概述
- 算法主要是由头文件
<algorithm><functional><numeric>组成。 <algorithm>是所有STL头文件中最大的一个,范围涉及到比较、 交换、查找、遍历操作、复制、修改等等。<numeric>体积很小,只包括几个在序列上面进行简单数学运算的模板函数。<functional>定义了一些模板类,用以声明函数对象。
二、常用遍历算法
1.for_each
功能描述: 实现遍历容器。
函数原型:
for_each(iterator beg, iterator end, _func);_func为函数或者函数对象。
2.transform
功能描述: 搬运容器到另一个容器中。
函数原型:
transform(iterator beg1, iterator end1, iterator beg2, _func);
3.代码
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Print{
public:
void operator()(int v){
cout << v << " ";
}
};
class Transform{
public:
int operator()(int v){
return v + 10;
}
};
void print(int v){
cout << v<<" ";
}
void test(){
vector<int>v;
for (int i = 0; i < 10; i++) v.push_back(i);
for_each(v.begin(), v.end(), print);//函数
cout << endl;
for_each(v.begin(), v.end(), Print());//函数对象
cout << endl;
vector<int>v1;
v1.resize(v.size());
//用transform搬运的目标容器必须要提前开辟空间,否则无法正常搬运
transform(v.begin(), v.end(), v1.begin(), Transform());
for_each(v1.begin(), v1.end(), Print());
cout << endl;
}
int main(){
test();
system("pause");
return 0;
}
运行效果:
三、常用查找算法
1.find
功能描述: 查找指定元素,找到返回指定元素的迭代器,找不到返回结束迭代器end()。
函数原型:
find(iterator beg, iterator end, value);value为查找的元素。
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
class Person{
string name;
int age;
public:
Person(string name, int age){
this->age = age;
this->name = name;
}
bool operator==(const Person &p){
if (this->age == p.age&&this->name == p.name) return true;
else return false;
}
};
void test(){
vector<int>v;
for (int i = 0; i < 10; i++) v.push_back(i);
vector<int>::iterator it = find(v.begin(), v.end(), 50);
if (it == v.end()) cout << "Not Found" << endl;
else cout << "Found" << endl;
Person p1("aa", 10);
Person p2("bb", 20);
Person p3("cc", 30);
Person p4("dd", 40);
Person p5("ee", 50);
vector<Person>v1;
v1.push_back(p1);
v1.push_back(p2);
v1.push_back(p3);
v1.push_back(p4);
v1.push_back(p5);
Person p("dd", 40);
vector<Person>::iterator it1 = find(v1.begin(), v1.end(), p);
if (it1 == v1.end()) cout << "Not Found" << endl;
else cout << "Found" << endl;
}
int main(){
test();
system("pause");
return 0;
}
运行效果:
2.find_if
功能描述: 按条件查找元素。
函数原型:
find_if(iterator beg, iterator end, _Pred);_Pred为 函数或者谓词(返回bool类型的仿函数)。
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
class Person{
public:
string name;
int age;
Person(string name, int age){
this->age = age;
this->name = name;
}
};
class Compare{
public:
int operator()(int v){
return v > 5;
}
};
class _Compare{
public:
bool operator()(const Person & p){
if (p.age == 50) return true;
else return false;
}
};
void test(){
vector<int>v;
for (int i = 0; i < 10; i++) v.push_back(i);
vector<int>::iterator it = find_if(v.begin(), v.end(), Compare());
if (it == v.end()) cout << "Not Found" << endl;
else cout << "Position: " << (*it) << endl;
Person p1("aa", 10);
Person p2("bb", 20);
Person p3("cc", 30);
Person p4("dd", 40);
Person p5("ee", 50);
vector<Person>v1;
v1.push_back(p1);
v1.push_back(p2);
v1.push_back(p3);
v1.push_back(p4);
v1.push_back(p5);
Person p("dd", 40);
vector<Person>::iterator it1 = find_if(v1.begin(), v1.end(), _Compare());
if (it1 == v1.end()) cout << "Not Found" << endl;
else cout << "Name: "<<(*it1).name<<" Age: "<<(*it1).age << endl;
}
int main(){
test();
system("pause");
return 0;
}
运行效果:
3.adjacent_find
功能描述: 查找相邻重复元素。
函数原型:
adjacent_find(iterator beg, iterator end);函数返回相邻重复元素的第一个位置的迭代器。
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
void test(){
vector<int>v;
v.push_back(0);
v.push_back(1);
v.push_back(0);
v.push_back(2);
v.push_back(2);
v.push_back(3);
v.push_back(3);
vector<int>::iterator it = adjacent_find(v.begin(), v.end());
if (it == v.end()) cout << "Not Found" << endl;
else cout << "Element: " << (*it) << endl;
}
int main(){
test();
system("pause");
return 0;
}
运行效果:
4.binary_search
功能描述: 查找指定元素是否存在(二分查找法)。
函数原型:
bool binary_search(iterator beg, iterator end, value);查找指定的元素,查到 返回true 否则false
注意: 在无序序列中不可用。
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void test(){
vector<int>v;
for (int i = 0; i < 10; i++) v.push_back(i);
bool it = binary_search(v.begin(), v.end(),9);
if (!it) cout << "Not Found" << endl;
else cout << "Found" << endl;
v.push_back(7);//需有序
bool it1 = binary_search(v.begin(), v.end(), 9);
if (!it1) cout << "Not Found" << endl;
else cout << "Found" << endl;
//无序时无法查找到
}
int main(){
test();
system("pause");
return 0;
}
运行效果:
5.count
功能描述: 统计元素个数。
函数原型:
count(iterator beg, iterator end, value);
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
class Person{
public:
string name;
int age;
Person(string name, int age){
this->age = age;
this->name = name;
}
bool operator==(const Person& p){
if (this->age == p.age) return true;
else return false;
}
};
void test(){
vector<int>v;
v.push_back(10);
v.push_back(10);
v.push_back(10);
int num = count(v.begin(), v.end(), 10);
cout << "count: " << num << endl;
Person p1("aa", 10);
Person p2("bb", 20);
Person p3("cc", 30);
Person p4("dd", 40);
Person p5("ee", 40);
vector<Person>v1;
v1.push_back(p1);
v1.push_back(p2);
v1.push_back(p3);
v1.push_back(p4);
v1.push_back(p5);
Person p("dd", 40);//疑似只要其一成员数据相同即可
num= count(v1.begin(), v1.end(),p);
//统计自定义数据类型时候,需要配合重载"operator=="
cout << "count: " << num << endl;
}
int main(){
test();
system("pause");
return 0;
}
运行效果:
6.count_if
功能描述: 按条件统计元素个数。
按值统计用count,按条件统计用count_if
函数原型:
count_if(iterator beg, iterator end, _Pred);
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
class Person{
public:
string name;
int age;
Person(string name, int age){
this->age = age;
this->name = name;
}
};
class AgeCompare{
public:
bool operator()(const Person& p){
if (p.age>=20) return true;
else return false;
}
};
class Compare{
public:
bool operator()(int v){
return v > 20;
}
};
void test(){
vector<int>v;
v.push_back(10);
v.push_back(20);
v.push_back(20);
int num = count_if(v.begin(), v.end(), Compare());
cout << "count: " << num << endl;
Person p1("aa", 10);
Person p2("bb", 20);
Person p3("cc", 30);
Person p4("dd", 40);
Person p5("ee", 40);
vector<Person>v1;
v1.push_back(p1);
v1.push_back(p2);
v1.push_back(p3);
v1.push_back(p4);
v1.push_back(p5);
num = count_if(v1.begin(), v1.end(), AgeCompare());
cout << "count: " << num << endl;
}
int main(){
test();
system("pause");
return 0;
}
运行效果:
四、常用排序算法
1.sort
功能描述: 对容器内元素进行排序。
函数原型:
sort(iterator beg, iterator end, _Pred);
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
void print(int v){
cout << v << " ";
}
void test(){
vector<int>v;
v.push_back(10);
v.push_back(30);
v.push_back(50);
v.push_back(40);
v.push_back(20);
sort(v.begin(), v.end());
for_each(v.begin(), v.end(), print);
cout << endl;
sort(v.begin(), v.end(),greater<int>());
for_each(v.begin(), v.end(), print);
cout << endl;
}
int main(){
test();
system("pause");
return 0;
}
运行效果:
2.random_shuffle
功能描述: 洗牌 指定范围内的元素随机调整次序。
函数原型:
random_shuffle(iterator beg, iterator end);
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <ctime>
using namespace std;
void print(int v){
cout << v << " ";
}
void test(){
srand((unsigned int)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(), print);
cout << endl;
}
int main(){
test();
system("pause");
return 0;
}
运行效果:
3.merge
功能描述: 两个容器元素合并,并存储到另一容器中。
函数原型:
merge(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);dest为目标容器开始迭代器,两个容器必须是有序的。
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void print(int v){
cout << v << " ";
}
void test(){
vector<int>v;
vector<int>v1;
vector<int>v2;
for (int i = 0; i < 10; i++) {
v.push_back(i);
v1.push_back(i + 1);
}
v2.resize(v.size() + v1.size());
merge(v.begin(), v.end(),v1.begin(),v1.end(),v2.begin());
for_each(v2.begin(), v2.end(), print);
cout << endl;
}
int main(){
test();
system("pause");
return 0;
}
运行效果:
4.reverse
功能描述: 将容器内元素进行反转。
函数原型:
reverse(iterator beg, iterator end);
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void print(int v){
cout << v << " ";
}
void test(){
vector<int>v;
v.push_back(10);
v.push_back(50);
v.push_back(40);
v.push_back(30);
v.push_back(20);
cout << "previous: " << endl;
for_each(v.begin(), v.end(), print);
cout << endl;
reverse(v.begin(), v.end());
cout << "later:" << endl;
for_each(v.begin(), v.end(), print);
cout << endl;
}
int main(){
test();
system("pause");
return 0;
}
运行效果:
五、常用拷贝和替换算法
1.copy
功能描述: 容器内指定范围的元素拷贝到另一容器中。
函数原型:
copy(iterator beg, iterator end, iterator dest);
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void print(int v){
cout << v << " ";
}
void test(){
vector<int> v;
for (int i = 0; i < 10; i++) v.push_back(i);
for_each(v.begin(), v.end(), print);
cout << endl;
vector<int>v1;
v1.resize(v.size());
copy(v.begin(), v.end(), v1.begin());
for_each(v1.begin(), v1.end(), print);
}
int main(){
test();
system("pause");
return 0;
}
运行效果:
2.replace
功能描述: 将容器内指定范围的旧元素修改为新元素。
函数原型:
replace(iterator beg, iterator end, oldvalue, newvalue);
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
class Print{
public:
void operator()(int v){
cout << v << " ";
}
};
void test(){
vector<int> v;
for (int i = 0; i < 10; i++) v.push_back(i);
cout << "Previous: ";
for_each(v.begin(), v.end(), Print());
cout << endl;
replace(v.begin(), v.end(), 2, 200);
cout << "Later: ";
for_each(v.begin(), v.end(), Print());
}
int main(){
test();
system("pause");
return 0;
}
运行效果:
3.replace_if
功能描述: 区间内满足条件的元素,替换成指定元素。
函数原型:
replace_if(iterator beg, iterator end, _pred, newvalue);
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
class Print{
public:
void operator()(int v){
cout << v << " ";
}
};
class Greater3{
public:
int operator()(int v){
return v >= 3;
}
};
void test(){
vector<int> v;
for (int i = 0; i < 10; i++) v.push_back(i);
cout << "Previous: ";
for_each(v.begin(), v.end(), Print());
cout << endl;
replace_if(v.begin(), v.end(), Greater30(), 200);
cout << "Later: ";
for_each(v.begin(), v.end(), Print());
}
int main(){
test();
system("pause");
return 0;
}
运行效果:
4.swap
功能描述: 互换两个容器的元素。
函数原型:
swap(container c1, container c2);
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
class Print{
public:
void operator()(int v){
cout << v << " ";
}
};
void test(){
vector<int> v;
vector<int> v1;
for (int i = 0; i < 10; i++) {
v.push_back(i);
v1.push_back(i + 100);
}
cout << "Previous: "<<endl;
for_each(v.begin(), v.end(), Print());
cout << endl;
for_each(v1.begin(), v1.end(), Print());
cout << endl;
swap(v, v1);
cout << "Later: "<<endl;
for_each(v.begin(), v.end(), Print());
cout << endl;
for_each(v1.begin(), v1.end(), Print());
}
int main(){
test();
system("pause");
return 0;
}
六、常用算术生产算法
- 该算法使用时头文件注意是
numeric
1.accumulate
功能描述: 计算区间内 容器元素累计总和。
函数原型:
accumulate(iterator beg, iterator end, value);value为初始值,即容器里的所有元素加起来再加上value。
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
void test(){
vector<int>v;
for (int i = 0; i <= 100; i++) v.push_back(i);
int total = accumulate(v.begin(), v.end(), 0);
cout << "Total: " << total<<endl;
total = accumulate(v.begin(), v.end(), 1000);
cout << "Total: " << total;
}
int main(){
test();
system("pause");
return 0;
}
2.fill
功能描述: 向容器中填充指定的元素。
函数原型:
fill(iterator beg, iterator end, value);
void print(int v){
cout << v << " ";
}
void test(){
vector<int>v;
v.resize(10);
fill(v.begin(), v.end(), 20);
for_each(v.begin(), v.end(), print);
}
int main(){
test();
system("pause");
return 0;
}
运行效果:
七、常用集合算法
1.set_intersection
功能描述: 求两个容器的交集。
- 目标容器开辟空间需要从两个容器中取小值。
- set_intersection返回值既是交集中最后一个元素的位置。
函数原型:
set_intersection(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);两个集合必须是有序序列
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void print(int v){
cout << v << " ";
}
void test(){
vector<int>v;
vector<int>v1;
vector<int>v2;
for (int i = 0; i < 10; i++){
v.push_back(i);
v1.push_back(i + 5);
}
v2.resize(min(v1.size(), v.size()));
vector<int>::iterator End = set_intersection(v.begin(), v.end(), v1.begin(), v1.end(), v2.begin());
for_each(v2.begin(), End, print);
//for_each(v2.begin(), v2.end(), print);
}
int main(){
test();
system("pause");
return 0;
}
运行效果:
2.set_union
功能描述: 求两个集合的并集。
- 目标容器开辟空间需要两个容器相加。
- set_union返回值既是并集中最后一个元素的位置。
函数原型:
set_union(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);两个集合必须是有序序列
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void print(int v){
cout << v << " ";
}
void test(){
vector<int>v;
vector<int>v1;
vector<int>v2;
for (int i = 0; i < 10; i++){
v.push_back(i);
v1.push_back(i + 5);
}
v2.resize(v1.size()+v.size());
vector<int>::iterator End = set_union(v.begin(), v.end(), v1.begin(), v1.end(), v2.begin());
for_each(v2.begin(), End, print);
}
int main(){
test();
system("pause");
return 0;
}
运行效果:
3.set_difference
功能描述: 求两个集合的差集。
- 目标容器开辟空间需要从两个容器取较大值。
- set_difference返回值既是差集中最后一个元素的位置。
函数原型:
set_difference(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);两个集合必须是有序序列。
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void print(int v){
cout << v << " ";
}
void test(){
vector<int>v;
vector<int>v1;
vector<int>v2;
for (int i = 0; i < 10; i++){
v.push_back(i);
v1.push_back(i + 5);
}
v2.resize(v1.size() + v.size());
vector<int>::iterator End = set_difference(v.begin(), v.end(), v1.begin(), v1.end(), v2.begin());
for_each(v2.begin(), End, print);
cout << endl;
End = set_difference(v1.begin(), v1.end(), v.begin(), v.end(), v2.begin());
for_each(v2.begin(), End, print);
cout << endl;
}
int main(){
test();
system("pause");
return 0;
}
运行效果: