STL常用算法

遍历算法

for_each

for_each(v.begin(),v.end(),print01);

cpp 复制代码
#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();
	return 0;
}

transform

cpp 复制代码
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//遍历算法 transform  搬运容器到另一个容器中
class Transform {
public:
	int operator()(int v) {
		return v;
	}
};
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();
	return 0;
}

查找算法

find

cpp 复制代码
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>
//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 << "找到:"<<*it << endl;
	}
}

class Person {
public:
	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;
		}
	}
	string m_Name;
	int m_Age;
};
//查找 自定义数据类型
void test02() {
	vector<Person>v;
	//创建数据
	Person p1("aaa", 10);
	Person p2("bbb", 30);
	Person p3("ccc", 40);

	//放入到容器中
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);

	Person pp("bbb", 30);

	vector<Person>::iterator it = find(v.begin(), v.end(), pp);//找和pp一样的
	if (it == v.end()) {
		cout << "未找到" << endl;
	}
	else {
		cout << "找到了  姓名:" << it->m_Name << " 年龄:" << it->m_Age << endl;
	}
}
int main() {
	test01();
	test02();
	return 0;
}

find_if 按条件查找

cpp 复制代码
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>
//find_if
//1.查找内置数据类型
class GreaterFive {//大于5的数
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 << "找到了>5的数据:" << *it << endl;
	}
}

//2.查找自定义数据类型
class Person {
public:
	Person(string name, int age) {
		this->m_Name = name;
		this->m_Age = age;
	}
	string m_Name;
	int m_Age;
};
class Greater25 {
public:
	bool operator()(Person& p) {
		return p.m_Age > 25;
	}
};
void test02() {
	vector<Person>v;
	//创建数据
	Person p1("tom", 27);
	Person p2("mak", 25);
	Person p3("san", 22);
	Person p4("li", 23);

	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);

	//找年龄>25的
	vector<Person>::iterator it = find_if(v.begin(), v.end(), Greater25());
	if (it == v.end()) {
		cout << "未找到" << endl;
	}
	else {
		cout << "找到了年龄>25的人 姓名:" << it->m_Name << endl;
	}
}
int main() {
	test01();
	test02();
	return 0;
}

adjacent_find 查找相邻重复元素

cpp 复制代码
#include<iostream>
using namespace std;
//adjacent_find  查找相邻重复元素
#include<vector>
#include<algorithm>
void test() {
	vector<int>v;
	v.push_back(5);
	v.push_back(2);
	v.push_back(4);
	v.push_back(5);//相邻重复
	v.push_back(5);

	vector<int>::iterator pos = adjacent_find(v.begin(), v.end());
	if (pos == v.end()) {
		cout << "未找到相邻重复元素" << endl;
	}
	else {
		cout << "找到了:" << *pos << endl;//输出元素的值
	}
}
int main() {
	test();
	return 0;
}
cpp 复制代码
#include<iostream>
using namespace std;
//binary_search 查找指定元素是否存在  条件:有序序列
#include<vector>
#include<algorithm>
void test() {
	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() {
	test();
	return 0;
}

count 统计元素个数

cpp 复制代码
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>
//count
//1.统计内置数据类型
void test01() {
	vector<int>v;
	v.push_back(5);
	v.push_back(2);
	v.push_back(4);
	v.push_back(5);
	v.push_back(5);

	int num = count(v.begin(),v.end(),5);//使用要包含算法的头文件

	cout << num << endl;
}

//2.统计自定义数据类型
class Person {
public:
	Person(string name, int age) {
		this->m_Name = name;
		this->m_Age = age;
	}

	//重载 == 让底层 count知道如何对比 person数据类型
	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("刘备", 23);//准备数据
	Person p2("关于", 21);
	Person p3("张飞", 26);
	Person p4("赵云", 21);

	//将人员插入到容器中
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);

	Person p5("诸葛亮", 21);
	int num = count(v.begin(), v.end(), p5);
	cout << "和诸葛亮同岁的人员个数为:" << num << endl;
}

int main() {
	test01();
	cout << "--------------------------------" << endl;
	test02();
	return 0;
}

count_if

按条件统计元素个数

cpp 复制代码
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>
//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(20);
	v.push_back(50);
	v.push_back(30);

	//统计>20的数
	int num = count_if(v.begin(), v.end(), Greater20());//Pred 写一个谓词
	cout << num << endl;//3
}

//2.统计自定义数据类型
class Person {
public:
	Person(string name, int age) {
		this->m_Name = name;
		this->m_Age = age;
	}
	string m_Name;
	int m_Age;
};

class AgeGreater20 {
public:
	bool operator()(const Person& p) {
		return p.m_Age > 21;
	}
};
void test02() {
	vector<Person>v;
	Person p1("刘备", 23);//准备数据
	Person p2("关于", 21);
	Person p3("张飞", 26);
	Person p4("赵云", 21);

	//将人员插入到容器中
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);

	//统计>21的数
	int num = count_if(v.begin(), v.end(), AgeGreater20());
	cout << num << endl;
}

int main() {
	test01();
	cout << "--------------------------------" << endl;
	test02();
	return 0;
}

排序算法

sort

第三个参数可以不填,默认升序

cpp 复制代码
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<functional>
void myPrint(int val) {
	cout << val << " ";
}
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(),myPrint);
	cout << endl;

	//sort降序
	sort(v.begin(), v.end(),greater<int>());
	for_each(v.begin(), v.end(), myPrint);
	cout << endl;
}
int main() {
	test01();
	return 0;
}

random_shuffle 洗牌 指定范围的元素随机调整次序

cpp 复制代码
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//random_shuffle 洗牌 指定范围的元素随机调整次序
#include<ctime>//time的头文件
void myPrint(int val) {
	cout << val << " ";
}
void test01() {
	srand((unsigned int)time(NULL));//随机数

	vector<int>v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(4);
	v.push_back(5);

	random_shuffle(v.begin(), v.end());
	for_each(v.begin(), v.end(), myPrint);
}
int main() {
	test01();
	return 0;
}

merge 合并两个有序容器

cpp 复制代码
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//merge
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>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;
}
int main() {
	test01();
	return 0;
}

reverse

拷贝和替换算法

copy

cpp 复制代码
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//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(v1.size());

	copy(v1.begin(), v1.end(), v2.begin());

	for_each(v2.begin(), v2.end(), myPrint);
	cout << endl;
}
int main() {
	test01();
	return 0;
}

replace

cpp 复制代码
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
class MyPrint {
public:
	void operator()(int val) {
		cout << val << " ";
	}
};
void test01() {
	vector<int>v;
	v.push_back(1);
	v.push_back(3);
	v.push_back(3);
	v.push_back(2);
	v.push_back(3);
	cout << "替换前" << endl;
	for_each(v.begin(), v.end(), MyPrint());
	cout << endl;

	cout << "替换后" << endl;
	replace(v.begin(), v.end(), 3, 3000);
	for_each(v.begin(), v.end(), MyPrint());
	cout << endl;
}
int main() {
	test01();
	return 0;
}

replace_if

将区间内满足条件的元素替换成指定元素

cpp 复制代码
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
class MyPrint {
public:
	void operator()(int val) {
		cout << val << " ";
	}
};
class Greater3 {//谓词
public:
	bool operator()(int val) {
		return val >= 3;
	}
};
void test01() {
	vector<int>v;
	v.push_back(1);
	v.push_back(3);
	v.push_back(3);
	v.push_back(2);
	v.push_back(4);
	cout << "替换前" << endl;
	for_each(v.begin(), v.end(), MyPrint());
	cout << endl;

	cout << "替换后" << endl;
	replace_if(v.begin(), v.end(), Greater3(), 3000);//第三个位置 谓词
	for_each(v.begin(), v.end(), MyPrint());
	cout << endl;
}
int main() {
	test01();
	return 0;
}

swap

互换两个同种类型的元素

cpp 复制代码
swap(v1, v2);

算术生成算法

accumulate

cpp 复制代码
#include<iostream>
using namespace std;
#include<vector>
#include<numeric>//accumulate头文件
void test() {
	vector<int>v;
	for (int i = 1; i <= 100; i++)
		v.push_back(i);//5050
	int total = accumulate(v.begin(), v.end(), 0);//起始累加值
	cout << total << endl;
}
int main() {
	test();
	return 0;
}

fill

cpp 复制代码
#include<iostream>
using namespace std;
#include<vector>
#include<numeric>//fill头文件
#include<algorithm>
void myPrint(int val) {
	cout << val << " ";
}
void test() {
	vector<int>v;
	v.resize(5);//指定大小

	//重新填充
	fill(v.begin(), v.end(), 100);//100 100 100 100 100
	for_each(v.begin(), v.end(), myPrint);
}
int main() {
	test();
	return 0;
}

集合算法

前提是两个容器是有序的

交集

并集

差集

cpp 复制代码
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//交集  set_intersection
//并集  set_union
void myPrint(int val) {
	cout << val << " ";
}
void test() {
	vector<int>v1;
	vector<int>v2;
	for (int i = 0; i < 10; i++) {
		v1.push_back(i);//0~9
		v2.push_back(i + 5);//5~14
	}

	//获取交集
	vector<int>vTarget;
	vTarget.resize(min(v1.size(),v2.size()));//最特殊情况 大容器包含小容器 两个中的较小值
	vector<int>::iterator itEnd = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
	for_each(vTarget.begin(), itEnd, myPrint);
	cout << endl;

	//获取并集
	vector<int>v1Target;
	v1Target.resize(v1.size()+v2.size());
	vector<int>::iterator it = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), v1Target.begin());
	for_each(v1Target.begin(), it, myPrint);
	cout << endl;

	//获取差集 v1和v2的
	vector<int>v2Target;
	v2Target.resize(max(v1.size(),v2.size()));
	vector<int>::iterator itd = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), v2Target.begin());
	for_each(v2Target.begin(), itd, myPrint);
	cout << endl;
}
int main() {
	test();
	return 0;
}
相关推荐
日暮南城故里2 分钟前
Java学习------源码解析之StringBuilder
java·开发语言·学习·源码
Vitalia29 分钟前
从零开始学Rust:枚举(enum)与模式匹配核心机制
开发语言·后端·rust
双叶8361 小时前
(C语言)虚数运算(结构体教程)(指针解法)(C语言教程)
c语言·开发语言·数据结构·c++·算法·microsoft
工一木子1 小时前
大厂算法面试 7 天冲刺:第5天- 递归与动态规划深度解析 - 高频面试算法 & Java 实战
算法·面试·动态规划
一个public的class3 小时前
什么是 Java 泛型
java·开发语言·后端
士别三日&&当刮目相看3 小时前
JAVA学习*Object类
java·开发语言·学习
invincible_Tang3 小时前
R格式 (15届B) 高精度
开发语言·算法·r语言
一只小松许️3 小时前
Rust闭包详解
开发语言·rust
独好紫罗兰3 小时前
洛谷题单2-P5715 【深基3.例8】三位数排序-python-流程图重构
开发语言·python·算法
牵牛老人3 小时前
C++设计模式-责任链模式:从基本介绍,内部原理、应用场景、使用方法,常见问题和解决方案进行深度解析
c++·设计模式·责任链模式