c++常用算法
目录
一. 遍历算法
1.for_each(起始迭代器,终点迭代器,函数());
2.transform(搬运且遍历)
二.查找算法
1.find` //查找元素
2.find_if` //按条件查找元素
3.adjacent_find` //查找相邻重复元素
4.binary_search` //二分查找法
5.count` //统计元素个数
6.count_if` //按条件统计元素个数
三.排序算法
1.sort
2random_shuffle()//.随机打乱
3.merge容器元素合并,并存储到另一容器中
4. reverse // 反转指定范围的元素
四.拷贝和替换算法
1.copy // 拷贝
2.replace // 将区间内旧元素 替换成 新元素
3.`replace_if // 按条件替换元素,满足条件的替换成指定元素
4.swap // 互换两个同种类型容器的元素
五.算数 算法
1.accumulate` // 计算容器元素累计总和
2.fill` // 向容器中添加元素
六.集合算法
1.set_intersection` // 求两个容器的交集
2.set_union` // 求两个容器的并集
3.set_difference ` // 求两个容器的差集
一. 遍历算法
1.for_each(起始迭代器,终点迭代器,函数());
作用:遍历迭代器,每一次都执行一遍函数。
#include<bits/stdc++.h>
using namespace std;
//普通函数
void print01(int val)
{
cout << val << " ";
}
//for_each算法基本用法
int main() {
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
//遍历算法
for_each(v.begin(), v.end(), print01);
cout << endl;
return 0;
}
2.transform(搬运且遍历)
* `transform(iterator beg1, iterator end1, iterator beg2, _func);`
//beg1 源容器开始迭代器
//end1 源容器结束迭代器
//beg2 目标容器开始迭代器
//_func 函数或者函数对象
#include<bits/stdc++.h>
using namespace std;
int trans(int val)
{
return (val+1000);
}
void printt01(int val)
{
cout << val << " ";
}
int main()
{
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(),trans);
for_each(vTarget.begin(),vTarget.end(),printt01);
//1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
return 0;
}
二.查找算法
1.find` //查找元素
功能描述: 查找指定元素,找到返回指定元素的迭代器,找不到返回结束迭代器end()
#include<bits/stdc++.h>
using namespace std;
void test01() {
vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back(i + 1);
}
//查找容器中是否有 5 这个元素
vector<int>::iterator it = find(v.begin(), v.end(), 5);
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;
}
//重载==
bool operator==(const Person& p)
{
if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
{
return true;
}
return false;
}
public:
string m_Name;
int m_Age;
};
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 << "找到姓名:" << it->m_Name << " 年龄: " << it->m_Age << endl;
}
}
int main()
{
test01();
test02();
return 0;
}
PS:所有的算法都类似,只要重载了operator函数,告诉底层代码如何比较即可运行自定义类型
2.find_if` //按条件查找元素
- `find_if(iterator beg, iterator end, _Pred); `
// 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
// beg 开始迭代器
// end 结束迭代器
// _Pred 函数或者谓词(返回bool类型的仿函数)
3.adjacent_find` //查找相邻重复元素
- `adjacent_find(iterator beg, iterator end); `
// 查找相邻重复元素,返回相邻元素的第一个位置的迭代器
// beg 开始迭代器
// end 结束迭代器
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(5);
v.push_back(2);
v.push_back(4);
v.push_back(4);
v.push_back(3);
//查找相邻重复元素
vector<int>::iterator it = adjacent_find(v.begin(), v.end());
if (it == v.end()) {
cout << "找不到!" << endl;
}
else {
cout << "找到相邻重复元素为:" << *it << endl;
}
return 0;
}
4.binary_search` //二分查找法
- `bool binary_search(iterator beg, iterator end, value); `
// 查找指定的元素,查到 返回true 否则false(bool类型)
// 注意: 在**无序序列中不可用,主要是用来判断有无某数据
// beg 开始迭代器
// end 结束迭代器
// value 查找的元素
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector<int>v;
//无序插入
v.push_back(1);
v.push_back(5);
v.push_back(4);
v.push_back(2);
v.push_back(3);
v.push_back(8);
//二分查找必须有序
sort(v.begin(),v.end());
//二分查找
bool ret = binary_search(v.begin(), v.end(),9);
if (ret)
{
cout << "找到了" << endl;
}
else
{
cout << "未找到" << endl;
}
return 0;
}
5.count` //统计元素个数
- `count(iterator beg, iterator end, value); `
// 统计元素出现次数
// beg 开始迭代器
// end 结束迭代器
// value 统计的元素
#include<bits/stdc++.h>
using namespace std;
//内置数据类型
void test01()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(4);
v.push_back(5);
v.push_back(3);
v.push_back(4);
v.push_back(4);
int num = count(v.begin(), v.end(), 4);
cout << "4的个数为: " << 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("刘备", 35);
Person p2("关羽", 35);
Person p3("张飞", 35);
Person p4("赵云", 30);
Person p5("曹操", 25);
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 = " << num << endl;
}
int main() {
test01();
test02();
return 0;
}
6.count_if` //按条件统计元素个数
- `count_if(iterator beg, iterator end, _Pred); `
// 按条件统计元素出现次数
// beg 开始迭代器
// end 结束迭代器
// _Pred 谓词(函数也行)
#include<bits/stdc++.h>
using namespace std;
bool Myrule(int val)
{
return val>=4;
}
int main()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(4);
v.push_back(5);
v.push_back(3);
v.push_back(4);
v.push_back(4);
int num = count_if(v.begin(), v.end(), Myrule);
cout << "大于等于4的个数为: " << num << endl;
return 0;
}
三.排序算法
1.sort
- `sort(iterator beg, iterator end, _Pred); `
// 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
// beg 开始迭代器
// end 结束迭代器
// _Pred 谓词
#include<bits/stdc++.h>
using namespace std;
//sort
void myPrint(int val)
{
cout << val << " ";
}
int Compare(int a,int b)
{
return a>b;
}
int main()
{
vector<int> v;
v.push_back(10);
v.push_back(30);
v.push_back(50);
v.push_back(20);
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>());
//或者自己写一个函数
sort(v.begin(), v.end(), Compare);
for_each(v.begin(), v.end(), myPrint);
cout << endl;
return 0;
}
2random_shuffle()//.随机打乱
- `random_shuffle(iterator beg, iterator end); `
// 指定范围内的元素随机调整次序
// beg 开始迭代器
// end 结束迭代器
#include<bits/stdc++.h>
using namespace std;
void Myprint(int val)
{
cout<< val<<" ";
}
int main()
{
//利用秒数生成随机数种子
srand((unsigned int)time(NULL));
vector<int> v;
for(int i = 0 ; i < 10;i++)
{
v.push_back(i);
}
for_each(v.begin(),v.end(),Myprint);
cout<<endl;
//随机打乱(伪随机),加上srand改变底层逻辑才是真随机
random_shuffle(v.begin(),v.end());
for_each(v.begin(),v.end(),Myprint);
return 0;
}
3.merge容器元素合并,并存储到另一容器中
merge(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest); `
// 容器元素合并,并存储到另一容器中
// 注意: 两个容器必须是有序的
// beg1 容器1开始迭代器
// end1 容器1结束迭代器
// beg2 容器2开始迭代器
// end2 容器2结束迭代器
// dest 目标容器开始迭代器
#include <bits/stdc++.h>
using namespace std;
//merge
void myPrint(int val)
{
cout << val << " ";
}
int main()
{
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(), myPrint);
cout << endl;
return 0;
}
4. reverse // 反转指定范围的元素
reverse(iterator beg, iterator end); `
// 反转指定范围的元素
// beg 开始迭代器
// end 结束迭代器
#include<bits/stdc++.h>
using namespace std;
//reverse
void myPrint(int val)
{
cout<< val <<" ";
}
int main()
{
vector<int> v;
v.push_back(10);
v.push_back(30);
v.push_back(50);
v.push_back(20);
v.push_back(40);
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;
return 0;
}
四.拷贝和替换算法
1.copy // 拷贝
copy(iterator beg, iterator end, iterator dest); `
// beg 开始迭代器
// end 结束迭代器
// dest 目标起始迭代器
//copy算法在拷贝时,目标容器记得提前开辟空间
一般不用,毕竟直接赋值v1=v2即可
#include<bits/stdc++.h>
using namespace std;
//adjacent_find
void myPrint(int val)
{
cout << val << " ";
}
int main()
{
vector<int> v1;
for (int i = 0; i < 10; i++) {
v1.push_back(i + 1);
}
vector<int> v2;
//copy算法在拷贝时,目标容器记得提前开辟空间
v2.resize(v1.size());
//拷贝
copy(v1.begin(), v1.end(), v2.begin());
//遍历打印
for_each(v2.begin(), v2.end(), myPrint);
cout << endl;
return 0;
}
2.replace // 将区间内旧元素 替换成 新元素
- `replace(iterator beg, iterator end, oldvalue, newvalue); `
// beg 开始迭代器
// end 结束迭代器
// oldvalue 旧元素
// newvalue 新元素
#include<bits/stdc++.h>
using namespace std;
//adjacent_find
void myPrint(int val)
{
cout << val << " ";
}
int main()
{
vector<int> v;
v.push_back(20);
v.push_back(30);
v.push_back(20);
v.push_back(40);
v.push_back(50);
v.push_back(10);
v.push_back(20);
cout << "替换前:" << endl;
for_each(v.begin(), v.end(), myPrint);
cout << endl;
//将容器中的20 替换成 2000
cout << "替换后:" << endl;
replace(v.begin(), v.end(), 20,2000);
for_each(v.begin(), v.end(), myPrint);
cout << endl;
return 0;
}
3.`replace_if // 按条件替换元素,满足条件的替换成指定元素
- `replace_if(iterator beg, iterator end, _pred, newvalue); `
// beg 开始迭代器
// end 结束迭代器
// _pred 谓词
// newvalue 替换的新元素
#include<bits/stdc++.h>
using namespace std;
//adjacent_find
void myPrint(int val)
{
cout << val << " ";
}
bool myRule(int val)
{
return val >=30;
}
int main()
{
vector<int> v;
v.push_back(20);
v.push_back(30);
v.push_back(20);
v.push_back(40);
v.push_back(50);
v.push_back(10);
v.push_back(20);
cout << "替换前:" << endl;
for_each(v.begin(), v.end(), myPrint);
cout << endl;
//将容器中大于等于的30 替换成 3000
cout << "替换后:" << endl;
replace_if(v.begin(), v.end(), myRule, 3000);
for_each(v.begin(), v.end(), myPrint);
cout << endl;
return 0;
}
4.swap // 互换两个同种类型容器的元素
- `swap(container c1, container c2); `
// c1容器1
// c2容器2
#include<bits/stdc++.h>
using namespace std;
//swap
void myPrint(int val)
{
cout << val << " ";
}
int main()
{
vector<int> v1;
vector<int> v2;
for (int i = 0; i < 10; i++) {
v1.push_back(i);
v2.push_back(i+100);
}
cout << "交换前: " << endl;
for_each(v1.begin(), v1.end(), myPrint);
cout << endl;
for_each(v2.begin(), v2.end(), myPrint);
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;
return 0;
}
五.算数 算法
1.accumulate` // 计算容器元素累计总和
- `accumulate(iterator beg, iterator end, value); `
// 计算容器元素累计总和
// beg 开始迭代器
// end 结束迭代器
// value 累加起始值
#include<bits/stdc++.h>
using namespace std;
//accumulate
int main()
{
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;
return 0;
}
2.fill` // 向容器中添加元素
- `fill(iterator beg, iterator end, value); `
// 向容器中填充元素
// beg 开始迭代器
// end 结束迭代器
// value 填充的值
#include<bits/stdc++.h>
using namespace std;
//adjacent_find
void myPrint(int val)
{
cout << val << " ";
}
int main()
{
vector<int> v;
for(int i=0;i<10;i++)
v.push_back(i);
cout<< "填充之前;"<<endl;
for_each(v.begin(), v.end(), myPrint);
cout<< endl;
//填充
fill(v.begin(), v.end(), 100);
cout<< "填充之后;"<<endl;
for_each(v.begin(), v.end(), myPrint);
cout << endl;
return 0;
}
六.集合算法
1.set_intersection` // 求两个容器的交集
- `set_intersection(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest); `
// 求两个集合的交集
// 注意:两个集合必须是有序序列,返回dest最后一个元素的迭代器
// beg1 容器1开始迭代器
// end1 容器1结束迭代器
// beg2 容器2开始迭代器
// end2 容器2结束迭代器
// dest 目标容器开始迭代器
#include<bits/stdc++.h>
using namespace std;
//set_intersection
void myPrint(int val)
{
cout<<val<<" ";
}
int main()
{
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(min(v1.size(),v2.size()));
//返回交集最后一个元素的迭代器
auto iend = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
//遍历打印
//如果终点迭代器是vTarget.end(),会吧容器内多出来的0打印出来
for_each(vTarget.begin(),iend,myPrint);
return 0;
}
//如果终点迭代器是vTarget.end(),会吧容器内多出来的0打印出来
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<bits/stdc++.h>
using namespace std;
//adjacent_find
void myPrint(int val)
{
cout<<val<<" ";
}
int main()
{
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());
//返回目标容器的最后一个元素的迭代器地址
auto itEnd =
set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
for_each(vTarget.begin(), itEnd, myPrint);
cout << endl;
return 0;
}
//如果终点迭代器是vTarget.end(),会吧容器内多出来的0打印出来
3.set_difference ` // 求两个容器的差集
差集:v1的差集:v1容器减去v1v2的交集
v2的差集:v2容器减去v1v2的交集
- `set_difference(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest); `
// 求两个集合的差集
// **注意:两个集合必须是有序序列**
// beg1 容器1开始迭代器
// end1 容器1结束迭代器
// beg2 容器2开始迭代器
// end2 容器2结束迭代器
// dest 目标容器开始迭代器
#include<bits/stdc++.h>
using namespace std;
//adjacent_find
void myPrint(int val)
{
cout<<val<<" ";
}
int main()
{
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;
//auto也可以
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 << "v2与v1的差集为: " << endl;
itEnd = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), vTarget.begin());
for_each(vTarget.begin(), itEnd, myPrint);
cout << endl;
return 0;
}