STL常用算法
概述:
算法主要由头文件<algorithm><functional><numeric>
组成
STL头文件中<algorithm>
是最大的一个,范围涉及到比较、交换、查找、遍历操作、复制、修改等等
for_each
#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);
}
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;
}
transform
将一个容器搬运到另一个容器中
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用的遍历算法 transform
//仿函数
class Transform {
public:
int operator()(int val) {
return val;
}
};
//仿函数
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(vTarget.begin(), vTarget.end(), MyPrint());
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
find
找到指定元素,返回指定元素的迭代器,找不到返回结束迭代器end()
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用的查找算法
//find
//查找 内置数据类型
void test01() {
vector<int > v;
for (int i = 0; i < 10; i++) {
v.push_back(i);
}
//查找容器中是否有 5 这个元素
vector<int> ::iterator pos=find(v.begin(), v.end(), 5);
if (pos == v.end()) {
cout << "没有找到" << endl;
}
else {
cout << "找到:" << *pos << endl;
}
}
class Person {
public:
string m_Name;
int m_Age;
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;
}
}
};
//查找 自定义数据类型
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);
vector<Person>::iterator it=find(v.begin(), v.end(), p2);
if (it == v.end()) {
cout << "没有找到" << endl;
}
else {
cout << "找到name: " << it->m_Name << " \tage: " << it->m_Age << endl;
}
}
int main() {
//test01();
test02();
system("pause");
return 0;
}
find_if
按条件查找元素
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用的查找算法 find_if
//1.查找内置数据类型
//仿函数
class GreaterFive {
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(), GreaterFive());
if (it == v.end()) {
cout << "没有找到" << endl;
}
else {
cout << "找到:" << *it << endl;
}
}
//2.查找自定义数据类型
class Person {
public:
string name;
int age;
Person(string name, int age) {
this->name = name;
this->age = age;
}
};
//仿函数
class Func {
public:
bool operator()(Person& p) {
return p.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(), Func());
if (it == v.end()) {
cout << "未找到" << endl;
}
else {
cout << "找到: \t姓名:" << it->name << "\t年龄: " << it->age << endl;
}
}
int main() {
//test01();
test02();
system("pause");
return 0;
}
adjacent_find
查找相邻重复元素
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
//查找算法 adjacent_find
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;
}
}
int main() {
test01();
system("pause");
return 0;
}
binary_search
查找效率很高
查找指定元素是否存在
在无序的序列中不可以使用
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用的查找算法 binary_search
void test01() {
vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back(i);
}
//v.push_back(2); //如果是无序的序列结果未知
//查找容器中是否有9
//注意:容器必须是有序的序列
bool ret=binary_search(v.begin(), v.end(), 9);
if (ret) {
cout << "找到了" << endl;
}
else {
cout << "没找到" << endl;
}
}
int main() {
test01();
system("pause");
return 0;
}
count
统计元素个数
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用的查找算法
//1.统计内置数据类型
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;
}
//2.统计自定义数据类型
class Person {
public:
string name;
int age;
Person(string name, int age) {
this->name = name;
this->age = age;
}
bool operator==(const Person& p) {
if (this->age == p.age) {
return true;
}
else {
return false;
}
}
};
void test02() {
vector<Person> v;
Person p1("刘备", 35);
Person p2("关羽", 35);
Person p3("张飞", 35);
Person p4("赵云", 30);
Person p5("曹操", 40);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
Person p("诸葛亮", 35);
int num=count(v.begin(), v.end(), p);
cout << "和诸葛亮同岁数的人员个数为:" << num << endl;
}
int main() {
//test01();
test02();
system("pause");
return 0;
}
count_if
按条件统计元素个数
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用查找算法 count_if
//统计内置数据类型
//仿函数 谓词
class Func {
public:
bool operator()(int val) {
if (val > 20) {
return true;
}
else {
return false;
}
}
};
void test01() {
vector<int> v;
v.push_back(10);
v.push_back(40);
v.push_back(30);
v.push_back(20);
v.push_back(40);
v.push_back(20);
int num=count_if(v.begin(), v.end(), Func());
cout << "大于20 的数的个数为:" << 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) {
// if (this->name == p.name && this->age == p.age) {
// return true;
// }
// else {
// return false;
// }
//}
};
//谓词
class Fff {
public:
bool operator()(const Person & p) {
if (p.age > 20) {
return true;
}
else {
return false;
}
}
};
void test02() {
vector<Person> v;
Person p1("aaa", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
Person p4("ddd", 40);
Person p5("eee", 50);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
count_if(v.begin(), v.end(), Fff());
}
int main() {
test01();
system("pause");
return 0;
}
常用排序算法
sort
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<functional>
void func(int val) {
cout << val << " ";
}
//class Greater{
//public:
// bool operator()(int v1, int v2) {
// return v1 > v2;
// }
//
//};
//常用排序算法
void test01() {
vector<int> v;
v.push_back(1);
v.push_back(3);
v.push_back(5);
v.push_back(2);
v.push_back(4);
//利用 sort进行排序
sort(v.begin(), v.end());
for_each(v.begin(), v.end(), func);
cout << endl;
//改变为降序
sort(v.begin(), v.end(), greater<int>());
for_each(v.begin(), v.end(), func);
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
random_shuffle
洗牌 指定范围内的元素随机调整次序
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<ctime>
void func(int val) {
cout << val << " ";
}
//洗牌算法 random_shuffle
void test01() {
vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back(i);
}
for_each(v.begin(), v.end(), func);
cout << endl;
//利用洗牌算法打乱顺序
random_shuffle(v.begin(), v.end());
for_each(v.begin(), v.end(), func);
}
int main() {
srand((unsigned int)time(NULL));
test01();
system("pause");
return 0;
}
merge
两个容器元素合并 并存储到另一个容器中
注意:两个容器必须是有序的
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
void func(int val) {
cout << val << endl;
}
//排序算法 merge
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> vTarget;
vTarget.resize(v1.size() + v2.size());
merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
for_each(vTarget.begin(), vTarget.end(), func);
}
int main() {
test01();
system("pause");
return 0;
}
reverse
将容器内元素反转
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
void func(int val) {
cout << val << " ";
}
//常用排序算法 reverse
void test01() {
vector<int> v;
v.push_back(1);
v.push_back(3);
v.push_back(5);
v.push_back(2);
v.push_back(4);
cout << "反转前:" << endl;
for_each(v.begin(), v.end(), func);
cout << endl;
reverse(v.begin(), v.end());
for_each(v.begin(), v.end(), func);
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
常用拷贝和替换算法
目标容器要提前开辟空间
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用拷贝和替换算法copy
void fun(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());
for_each(v2.begin(), v2.end(), fun);
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
replace
将容器内指定范围的旧元素改为新元素
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void func(int val) {
cout << val << " ";
}
//常用拷贝和替换算法 replace
void test01() {
vector<int > v;
v.push_back(20);
v.push_back(30);
v.push_back(50);
v.push_back(10);
v.push_back(40);
v.push_back(20);
v.push_back(20);
v.push_back(20);
cout << "替换前:" << endl;
for_each(v.begin(), v.end(), func);
cout << endl;
//把所有的20替换为10000
replace(v.begin(), v.end(), 20, 10000);
for_each(v.begin(), v.end(), func);
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
replace_if
将区间内满足条件的元素,替换成指定元素
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
void func(int val) {
cout << val << " ";
}
class Pred {
public:
bool operator()(int v) {
return v > 10;
}
};
//常用的拷贝和替换算法 replace_if
void test01() {
vector<int> v;
v.push_back(10);
v.push_back(60);
v.push_back(20);
v.push_back(80);
v.push_back(50);
v.push_back(30);
v.push_back(40);
v.push_back(70);
for_each(v.begin(), v.end(), func);
cout << endl;
replace_if(v.begin(), v.end(), Pred(), 1000);//满足Pred的替换为1000
for_each(v.begin(), v.end(), func);
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
swap
互换两个容器的元素
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
void func(int val) {
cout << val << " ";
}
//swap
void test01() {
vector<int> v1,v2;
for (int i = 0; i < 10; i++) {
v1.push_back(i);
v2.push_back(i + 100);
}
for_each(v1.begin(), v1.end(), func);
cout << endl;
for_each(v2.begin(), v2.end(), func);
cout << endl;
cout << "------------------------交换后---------------------------------" << endl;
swap(v1, v2);
for_each(v1.begin(), v1.end(), func);
cout << endl;
for_each(v2.begin(), v2.end(), func);
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
常用算数生成算法
头文件#include<numeric>
accumulate
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<numeric>
void test01() {
vector<int> v;
for (int i = 0; i <= 100; i++) {
v.push_back(i);
}
//参数3 是一个起始累加值
int total = accumulate(v.begin(), v.end(),0);
cout << "total=" << total << endl;
}
int main() {
test01();
system("pause");
return 0;
}
fill
向容器中填充指定元素
#include<iostream>
using namespace std;
#include<numeric>
#include<vector>
#include<algorithm>
void func(int val) {
cout << val << " ";
}
//fill
void test01() {
vector<int> v;
v.resize(10);//自动填充0
//后期重新填充
fill(v.begin(), v.end(), 100);
for_each(v.begin(), v.end(), func);
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
常用集合算法
set_intersection
求交集
原来两个容器要有序
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
void fun(int val) {
cout << val << " ";
}
//set_intersection 求交集
void test01() {
vector<int> v1,v2,vTarget;
for (int i = 0; i < 10; i++) {
v1.push_back(i);//0~9
v2.push_back(i + 5);//5~14
}
//目标容器需要提前开辟空间
//最特殊情况为 大容器包含小容器 开辟空间 取小容器的size即可
vTarget.resize(min(v1.size(),v2.size()));
//获取交集
vector<int>::iterator it =set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
for_each(vTarget.begin(), it, fun);//结束迭代器要用调用set_intersection返回的结束迭代器 因为vTarget.end() 的结束位置是开辟的整个vTarget 而这个大小是一种特殊情况
}
int main() {
test01();
system("pause");
return 0;
}
set_union
求并集
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
void func(int val) {
cout << val << " ";
}
//set_union
void test01() {
vector<int> v1,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, func);
}
int main() {
test01();
system("pause");
return 0;
}
set_difference
求差集
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
void func(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;
vTarget.resize(max(v1.size(), v2.size()));
cout << "v1和v2的差集为:" << endl;
vector<int>::iterator it=set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
for_each(vTarget.begin(), it, func);
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}