概述:
算法主要由头文件 <algorithm> <functional> <numeric> 组成
<algorithm> 是所有STL文件中最大的一个 范围涉及到 比较 交换 查找 遍历 复制 修改 等
<numeric> 体积很小 只包括几个在序列上面进行简单数学运算的模板函数
<functional> 定义了一些模板类 用于声明函数对象
1. 常用遍历算法
1.1 for_each
功能描述:
实现遍历容器
常用
代码示例
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
//常用遍历算法 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);
}
for_each(v.begin(), v.end(), print01); //0 1 2 3 4 5 6 7 8 9
cout << endl;
for_each(v.begin(), v.end(), print02()); //0 1 2 3 4 5 6 7 8 9
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
1.2 transform
功能描述 :
搬运一个容器到另一个容器
搬运时目标容器一定要提前开辟空间
代码示例
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
//常用遍历算法 transfrom
class Transfrom
{
public:
int operator()(int val)
{
return val+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(10); //目标容器需要提前开辟空间
transform(v.begin(), v.end(), vTarget.begin(), Transfrom());
for_each(vTarget.begin(), vTarget.end(), MyPrint());
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
2. 常用查找算法
2.1 find
功能描述:
查找指定容器 找到返回指定元素的迭代器 找不到返回end()
find 查找自定义数据类型时 需要重载==
代码示例
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
//常用查找算法 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 << "没有找到" << endl;
}
else
{
cout << "找到了" << endl;
}
}
//查找自定义数据类型
class Person
{
public:
Person(string name, int age)
{
this->m_age = age;
this->m_name = name;
}
bool operator==(const Person & p)
{
if (this->m_name == p.m_name && this->m_age == p.m_age)
{
return true;
}
return false;
}
string m_name;
int m_age;
};
void test02()
{
vector<Person> v;
Person p1("aa", 10);
Person p2("bb", 20);
Person p3("cc", 30);
Person p4("dd", 40);
Person pp("bb", 20);
//放到容器中
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
vector<Person>::iterator it = find(v.begin(), v.end(), pp);
if (it == v.end())
{
cout << "没有找到" << endl;
}
else
{
cout << "找到了" << endl;
cout << "姓名:" << it->m_name << " 年龄:" << it->m_age << endl;
}
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
2.2 find_if
功能描述:
按条件查找元素
按元素查找 找到返回指定位置的迭代器 找不到返回结束迭代器
代码示例
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
//常用查找算法 find_if
//查找内置数据类型
class GreateFive
{
public:
bool operator()(int val)
{
return val > 5;
}
};
void test01()
{
vector<int> v;
for (int i = 0;i < 10;i++)
{
v.push_back(i);
}
//查找容器中是否有 5 这个元素
vector<int>::iterator it = find_if(v.begin(), v.end(), GreateFive());
if(it == v.end())
{
cout << "没有找到" << endl;
}
else
{
cout << "找到大于5的数字为:"<<*it << endl; //6
}
}
//查找自定义数据类型
class Person
{
public:
Person(string name, int age)
{
this->m_age = age;
this->m_name = name;
}
bool operator==(const Person & p)
{
if (this->m_name == p.m_name && this->m_age == p.m_age)
{
return true;
}
return false;
}
string m_name;
int m_age;
};
class Greater20
{
public:
bool operator()(Person &p)
{
return p.m_age > 20;
}
};
void test02()
{
vector<Person> v;
Person p1("aa", 10);
Person p2("bb", 20);
Person p3("cc", 30);
Person p4("dd", 40);
Person pp("bb", 20);
//放到容器中
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(), Greater20());
if (it == v.end())
{
cout << "没有找到" << endl;
}
else
{
cout << "找到了" << endl;
cout << "姓名:" << it->m_name << " 年龄:" << it->m_age << endl;
}
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
3. adjacent_find
功能描述:
查找相邻重复元素
返回相邻元素的第一个迭代器位置
代码示例
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
//常用查找算法 adjacent_if
//查找相邻重复元素
void test01()
{
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);
vector<int>::iterator it = adjacent_find(v.begin(), v.end());
if(it == v.end())
{
cout << "没有找到相邻重复元素" << endl;
}
else
{
cout << "找到了相邻重复元素:"<<*it << endl; //3
}
}
int main()
{
test01();
system("pause");
return 0;
}
4. binary_search
功能描述:
查找指定元素是否存在
该算法查找效率很高 但是必须在有序的序列中使用
代码示例
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
//常用查找算法 binary_search
void test01()
{
vector<int> v;
for (int i = 0;i < 10;i++)
{
v.push_back(i);
}
//查找容器中是否有9元素
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
功能描述:
统计元素个数
统计自定义数据类型 需要重载 ==
代码示例
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
//常用查找算法 count
//1、统计内置数据类型
void test01()
{
vector<int> v;
v.push_back(10);
v.push_back(40);
v.push_back(40);
v.push_back(20);
v.push_back(40);
v.push_back(30);
int num = count(v.begin(), v.end(), 40);
cout << "40的元素个数是:"<<num << endl; //3
}
//2、统计自定义数据类型
class Person
{
public:
Person(string name, int age)
{
this->m_age = age;
this->m_name = name;
}
bool operator ==(const Person &p)
{
if (this->m_age == p.m_age)
{
return true;
}
return false;
}
string m_name;
int m_age;
};
void test02()
{
vector<Person> v;
Person p1("刘备", 35);
Person p2("关羽", 35);
Person p3("张飞", 35);
Person p4("曹操", 40);
Person p5("赵云", 20);
Person p6("诸葛亮", 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 << "和诸葛亮同岁人个数是:" << num << endl; //3
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
6. count_if
功能描述:
按条件统计元素个数
代码示例
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
//常用查找算法 count_if
//1、统计内置数据类型
class Greater20
{
public:
bool operator()(int val)
{
return val > 20;
}
};
void test01()
{
vector<int> v;
v.push_back(10);
v.push_back(40);
v.push_back(40);
v.push_back(20);
v.push_back(40);
v.push_back(30);
int num = count_if(v.begin(), v.end(), Greater20());
cout << "容器中大于20的元素个数是:"<<num << endl; //4
}
//2、统计自定义数据类型
class Person
{
public:
Person(string name, int age)
{
this->m_age = age;
this->m_name = name;
}
bool operator ==(const Person &p)
{
if (this->m_age == p.m_age)
{
return true;
}
return false;
}
string m_name;
int m_age;
};
class AgeGreater20
{
public:
bool operator()(const Person &p)
{
return p.m_age > 20;
}
};
void test02()
{
vector<Person> v;
Person p1("刘备", 35);
Person p2("关羽", 35);
Person p3("张飞", 35);
Person p4("曹操", 40);
Person p5("赵云", 20);
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(), AgeGreater20());
cout << "大于20岁的人的个数是:" << num << endl; //4
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
3. 常用排序算法
3.1 sort
功能描述:
对容器中的数据进行排序
很常用
代码示例
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<algorithm>
#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(40);
v.push_back(20);
v.push_back(40);
v.push_back(30);
//利用sort进行升序
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;
}
3.2 random_shuffle
功能描述:
指定范围内的数据随机打乱
增加随机数种子 确保真实随机
代码示例
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
#include<ctime>
//常用排序算法 random_shuffle
void myPrint(int val)
{
cout << val << " ";
}
void test01()
{
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(), myPrint);
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
3.3 merge
功能描述:
将两个容器元素合并到一个容器
必须是两个有序容器 合成的结果也是有序的
使用前切记给目标容器开辟空间
代码示例
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
#include<ctime>
//常用排序算法 random_shuffle
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+1);
}
//目标容器
vector<int> v;
v.resize(20);
merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v.begin());
for_each(v.begin(), v.end(),MyPrint); //0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
3.4 reverse
功能描述:
将容器内元素反转
代码示例
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
#include<ctime>
//常用排序算法 reverse
void MyPrint(int val)
{
cout << val << " ";
}
void test01()
{
vector<int> v1;
vector<int> v2;
v1.push_back(50);
v1.push_back(30);
v1.push_back(20);
v1.push_back(40);
v1.push_back(10);
cout << "反转前" << endl;
for_each(v1.begin(), v1.end(),MyPrint); //0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10
cout << endl;
reverse(v1.begin(), v1.end());
cout << "反转后" << endl;
for_each(v1.begin(), v1.end(), MyPrint); //0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
4. 常用拷贝算法
4.1 copy
功能描述:
容器内指定范围的元素拷贝到另一容器中
目标容器记得提前开辟空间
代码示例
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
#include<ctime>
//常用拷贝和替换算法 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(10);
copy(v1.begin(), v1.end(), v2.begin());
for_each(v1.begin(), v1.end(),MyPrint); //0 1 2 3 4 5 6 7 8 9
cout << endl;
for_each(v2.begin(), v2.end(), MyPrint); //0 1 2 3 4 5 6 7 8 9
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
4.2 replace
功能描述:
将容器内指定范围的旧元素更换为新元素
会替换区间内所有满足条件的元素
代码示例
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
#include<ctime>
//常用拷贝和替换算法 copy
void MyPrint(int val)
{
cout << val << " ";
}
void test01()
{
vector<int> v1;
for (int i = 0;i < 9;i++)
{
v1.push_back(i);
}
v1.push_back(1);
cout << "替换前:" << endl;
for_each(v1.begin(), v1.end(),MyPrint); //0 1 2 3 4 5 6 7 8 1
cout << endl;
//替换
replace(v1.begin(), v1.end(), 1, 100);
cout << "替换后:" << endl;
for_each(v1.begin(), v1.end(), MyPrint); //0 100 2 3 4 5 6 7 8 100
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
4.3 replace_if
功能描述:
将区间内满足条件的元素 替换为新元素
利用仿函数可以灵活调整替换条件
代码示例
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
#include<ctime>
//常用拷贝和替换算法 copy
void MyPrint(int val)
{
cout << val << " ";
}
class Greater30
{
public:
bool operator ()(int val)
{
return val >= 3;
}
};
void test01()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(4);
v1.push_back(5);
v1.push_back(3);
v1.push_back(6);
v1.push_back(2);
v1.push_back(7);
v1.push_back(4);
//大于等于3的替换为13
cout << "替换前:" << endl;
for_each(v1.begin(), v1.end(),MyPrint); //1 4 5 3 6 2 7 4
cout << endl;
//替换
replace_if(v1.begin(), v1.end(), Greater30(), 13);
cout << "替换后:" << endl;
for_each(v1.begin(), v1.end(), MyPrint); //1 13 13 13 13 2 13 13
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
4.4 swap
功能描述:
互换两个容器的元素
必须是两个同种容器
代码示例
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
#include<ctime>
//常用拷贝和替换算法 copy
void MyPrint(int val)
{
cout << val << " ";
}
class Greater30
{
public:
bool operator ()(int val)
{
return val >= 3;
}
};
void test01()
{
vector<int> v1;
vector<int> v2;
v1.push_back(1);
v1.push_back(4);
v1.push_back(5);
v1.push_back(3);
v1.push_back(6);
v2.push_back(10);
v2.push_back(40);
v2.push_back(50);
v2.push_back(30);
v2.push_back(60);
cout << "交换前:" << endl;
for_each(v1.begin(), v1.end(),MyPrint); //1 4 5 3 6
cout << endl;
for_each(v2.begin(), v2.end(),MyPrint); //10 40 50 30 60
cout << endl;
swap(v1,v2);
//替换
cout << "交换后:" << endl;
for_each(v1.begin(), v1.end(), MyPrint); //10 40 50 30 60
cout << endl;
for_each(v2.begin(), v2.end(), MyPrint); //1 4 5 3 6
cout << endl;
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
5. 常用算术生成算法
算术生成算法属于小型算法 需要包含头文件 <numeric>
5.1 accumulate
功能描述:
计算区间内容器元素累计求和
函数的第三个参数是一个起始的累加值
代码示例
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<numeric>
//常用拷贝和替换算法 copy
void test01()
{
vector<int> v1;
for (int i = 0;i <= 100;i++)
{
v1.push_back(i);
}
//参数3是一个起始的累加值
int total = accumulate(v1.begin(), v1.end(), 0);
cout << total << endl; //5050
}
int main()
{
test01();
system("pause");
return 0;
}
5.2 fill
功能描述:
向容器中填充指定元素
代码示例
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<numeric>
#include<algorithm>
//常用拷贝和替换算法 copy
void MyPrint(int val)
{
cout << val << " ";
}
void test01()
{
vector<int> v1;
v1.resize(10);
//后期重新填充
fill(v1.begin(), v1.end(), 100);
for_each(v1.begin(), v1.end(), MyPrint); //100 100 100 100 100 100 100 100 100 100
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
6. 常用集合算法
6.1 set_intersection
功能描述:
获取两个容器的交集
求交集的两个容器必须是有序序列
目标容器的开辟空间需要从两个容器中取最小值
set_intersection 算法的返回值是交集中最后一个元素的位置
代码示例
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<numeric>
#include<algorithm>
//常用集合算法 set_intersection
void MyPrint(int val)
{
cout << val << " ";
}
class Greater30
{
public:
bool operator ()(int val)
{
return val >= 3;
}
};
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> v;
v.resize(min(size(v1), size(v2)));
//获取交集
vector<int>::iterator itend = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), v.begin());
for_each(v.begin(), itend, MyPrint); //5 6 7 8 9
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
6.2 set_union
功能描述:
获取两个容器的并集
求并集的两个容器必须是有序序列
目标容器的开辟空间应该是两个容器的 size 相加
set_union 算法的返回值是并集中最后一个元素的位置
代码示例
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<numeric>
#include<algorithm>
//常用集合算法 set_union
void MyPrint(int val)
{
cout << val << " ";
}
class Greater30
{
public:
bool operator ()(int val)
{
return val >= 3;
}
};
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> v;
v.resize(size(v1)+size(v2));
vector<int>::iterator itend = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), v.begin());
for_each(v.begin(), itend, MyPrint); //0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
6.3 set_difference
功能描述:
获取两个容器的并集
两个容器的顺序会影响结果
求并集的两个容器必须是有序序列
目标容器的开辟空间应该是两个容器的 size 中较大的值
set_difference 算法的返回值是并集中最后一个元素的位置
代码示例
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<numeric>
#include<algorithm>
//常用集合算法 set_difference
void MyPrint(int val)
{
cout << val << " ";
}
class Greater30
{
public:
bool operator ()(int val)
{
return val >= 3;
}
};
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> v;
v.resize(max(size(v1),size(v2)));
cout << "v1 和 v2 的差集:" << endl;
vector<int>::iterator itend = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), v.begin());
for_each(v.begin(), itend, MyPrint); //0 1 2 3 4
cout << endl;
cout << "v2 和 v1 的差集:" << endl;
itend = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), v.begin());
for_each(v.begin(), itend, MyPrint); //10 11 12 13 14
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
754



被折叠的 条评论
为什么被折叠?



