栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

【C++】STL常用算法

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【C++】STL常用算法

遍历算法 for_each(iterator beg, iterator end, _func);

遍历容器。

transform(iterator beg1, iterator end1, iterator beg2, _func)

搬运容器,目标容器需要提前开辟空间。

#include 
#include 
#include 
using namespace std;

class Print
{
public:
	void operator()(int val) {
		cout << val << " ";
	}
};

void test01() {
	vector vec = { 0,1,2,3,4,5,6,7,8,9 };
	for_each(vec.begin(), vec.end(), Print());
	cout << endl;
}

class Transform
{
public:
	int operator()(int val) {
		return val;
	}
};

void test02()
{
	vector vec = { 0,1,2,3,4,5,6,7,8,9 };
	// 目标容器
	vector vTarget;
	// 目标容器需要提前开辟空间
	vTarget.resize(vec.size());

	transform(vec.begin(), vec.end(), vTarget.begin(), Transform());
	for_each(vTarget.begin(), vTarget.end(), Print());
}

int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}
查找算法 find(iterator beg, iterator end, value);

按值查找元素,找到返回指定位置的迭代器,找不到返回尾后迭代器。 查找自定义数据类型时,确保该类型定义了==运算符。

#include 
#include 
#include 
#include 
using namespace std;

// 查找内置数据类型
void test01()
{
	vector vec = { 0,1,2,3,4,5,6,7,8,9 };
	// 查找容器中是否有5这个元素
	vector::iterator it = find(vec.begin(), vec.end(), 5);
	if (it == vec.end()) {
		cout << "没有找到!" << endl;
	}
	else {
		cout << "找到!" << 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 vec;
	vec.emplace_back("aaa", 10);
	vec.emplace_back("bbb", 20);
	vec.emplace_back("ccc", 30);
	vec.emplace_back("ddd", 40);

	vector::iterator it = find(vec.begin(), vec.end(), Person("aaa", 10));
	if (it == vec.end()) {
		cout << "没有找到!" << endl;
	}
	else {
		cout << "找到!" << endl;
	}
}

int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}
find_if(iterator beg, iterator end, _Pred);

按值查找元素,找到返回指定位置的迭代器,找不到返回尾后迭代器。

#include 
#include 
#include 
#include 
using namespace std;

// 查找内置数据类型
class GreaterFive
{
public:
	bool operator()(int val) {
		return val > 5;
	}
};

void test01()
{
	vector vec = { 0,1,2,3,4,5,6,7,8,9 };
	vector::iterator it = find_if(vec.begin(), vec.end(), GreaterFive());
	if (it == vec.end()) {
		cout << "没有找到!" << endl;
	}
	else {
		cout << "找到!" << endl;
	}
}

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

class Greater20
{
public:
	bool operator()(Person& p)
	{
		return p.m_Age > 20;
	}
};

void test02() {

	vector vec;

	// 创建数据
	vec.emplace_back("aaa", 10);
	vec.emplace_back("bbb", 20);
	vec.emplace_back("ccc", 30);
	vec.emplace_back("ddd", 40);

	// 查找年龄大于20的人
	vector::iterator it = find_if(vec.begin(), vec.end(), Greater20());
	if (it == vec.end()) {
		cout << "没有找到!" << endl;
	}
	else {
		cout << "找到!" << endl;
	}
}

int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}
adjacent_find(iterator beg, iterator end);

查找相邻重复元素,返回相邻元素的第一个位置的迭代器,找不到返回尾后迭代器。

bool binary_search(iterator beg, iterator end, value);

利用二分查找法查找指定的元素,在无序序列中不可用!

#include 
#include 
#include 
#include 
using namespace std;

void test01()
{
	vector vec = { 1,2,5,2,4,4,3 };

	// 查找相邻重复元素
	vector::iterator it = adjacent_find(vec.begin(), vec.end());
	if (it == vec.end()) {
		cout << "找不到!" << endl;
	}
	else {
		cout << "找到!" << endl;
	}

	sort(vec.begin(), vec.end());
	// 查找有序容器中是否有元素2
	bool ret = binary_search(vec.begin(), vec.end(), 2);
	if (ret) {
		cout << "找到!" << endl;
	}
	else {
		cout << "未找到!" << endl;
	}
}

int main()
{
	test01();
	system("pause");
	return 0;
}
count(iterator beg, iterator end, value);

统计元素出现次数。

#include 
#include 
#include 
#include 
using namespace std;

// 统计内置数据类型
void test01()
{
	vector v = { 1,2,4,5,3,4,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;
		}
		return false;
	}
public:
	string m_Name;
	int m_Age;
};

void test02()
{
	vector v;
	v.emplace_back("刘备", 35);
	v.emplace_back("关羽", 35);
	v.emplace_back("张飞", 35);
	v.emplace_back("赵云", 30);
	v.emplace_back("曹操", 25);

	Person p("诸葛亮", 35);
	int num = count(v.begin(), v.end(), p);
	cout << "num = " << num << endl;
}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}
count_if(iterator beg, iterator end, _Pred);

按条件统计元素出现次数。

#include 
#include 
#include 
using namespace std;

class Greater4
{
public:
	bool operator()(int val)
	{
		return val >= 4;
	}
};

// 统计内置数据类型
void test01()
{
	vector vec = { 1,2,4,5,3,4,4 };
	int num = count_if(vec.begin(), vec.end(), Greater4());
}

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

class AgeLess35
{
public:
	bool operator()(const Person& p)
	{
		return p.m_Age < 35;
	}
};

void test02()
{
	vector vec;
	vec.emplace_back("刘备", 35);
	vec.emplace_back("关羽", 35);
	vec.emplace_back("张飞", 35);
	vec.emplace_back("赵云", 30);
	vec.emplace_back("曹操", 25);

	int num = count_if(vec.begin(), vec.end(), AgeLess35());
}


int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}
常用排序算法 sort(iterator beg, iterator end, _Pred);

对容器内元素进行排序,默认按升序排序。

#include 
#include 
#include 
using namespace std;

void test01()
{
	vector vec = { 10,30,50,20,40 };
	// 默认升序排序
	sort(vec.begin(), vec.end());
	for (auto v : vec)
		cout << v << " ";
	cout << endl;	// 10 20 30 40 50

	// 设置降序排序
	sort(vec.begin(), vec.end(), greater());
	for (auto v : vec)
		cout << v << " ";
	cout << endl;	// 50 40 30 20 10
}

int main() {
	test01();
	system("pause");
	return 0;
}
random_shuffle(iterator beg, iterator end);

指定范围内的元素随机调整次序。

#include 
#include 
#include 
#include 
using namespace std;

void test01()
{
	// 随机数种子
	srand((unsigned int)time(NULL));
	vector vec = { 0,1,2,3,4,5,6,7,8,9 };
	for_each(vec.begin(), vec.end(), [](int val) {cout << val << " "; });
	cout << endl;	// 0 1 2 3 4 5 6 7 8 9

	// 利用洗牌算法 打乱顺序
	random_shuffle(vec.begin(), vec.end());
	for_each(vec.begin(), vec.end(), [](int val) {cout << val << " "; });
	cout << endl;	// 7 6 5 2 3 9 8 4 1 0
}

int main() {
	test01();
	system("pause");
	return 0;
}
merge(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);

两个容器元素合并,并存储到另一容器中,目标容器需要提前开辟空间,两个容器必须是有序的!

#include 
#include 
#include 
using namespace std;

void test01()
{
	vector vec1 = { 0,1,2,3,4,5,6,7,8,9 };
	vector vec2 = { 1,2,3,4,5,6,7,8,9,10 };
	// 目标容器
	vector vtarget;
	// 目标容器需要提前开辟空间
	vtarget.resize(vec1.size() + vec2.size());
	// 合并两个有序序列
	merge(vec1.begin(), vec1.end(), vec2.begin(), vec2.end(), vtarget.begin());
	for (auto v : vtarget)
		cout << v << " ";
	cout << endl;	// 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10
}

int main() {
	test01();
	system("pause");
	return 0;
}
reverse(iterator beg, iterator end);

将容器内元素进行反转。

#include 
#include 
#include 
using namespace std;

void test01()
{
	vector vec = { 1,2,3,4,5,6,7,8,9 };

	cout << "反转前: " << endl;
	for (auto v : vec)
		cout << v << " ";
	cout << endl;	// 1 2 3 4 5 6 7 8 9

	reverse(vec.begin(), vec.end());
	cout << "反转后: " << endl;
	for (auto v : vec)
		cout << v << " ";
	cout << endl;	// 9 8 7 6 5 4 3 2 1
}

int main() 
{
	test01();
	system("pause");
	return 0;
}
常用拷贝和替换算法 copy(iterator beg, iterator end, iterator dest);

容器内指定范围的元素拷贝到另一容器中,目标容器需要提前开辟空间。

#include 
#include 
#include 
using namespace std;

void test01()
{
	vector vec1 = { 0,1,2,3,4,5,6,7,8,9 };
	vector vec2;
	// 目标容器提前开辟空间
	vec2.resize(vec1.size());
	copy(vec1.begin(), vec1.end(), vec2.begin());
	for (auto v : vec2)
		cout << v << " ";
	cout << endl;	// 0 1 2 3 4 5 6 7 8 9
}

int main()
{
	test01();
	system("pause");
	return 0;
}
replace(iterator beg, iterator end, oldvalue, newvalue);

将容器内指定范围的旧元素修改为新元素。

#include 
#include 
#include 
using namespace std;

void test01()
{
	vector vec = { 20,30,20,40,50,10,20 };
	cout << "替换前:" << endl;
	for (auto v : vec)
		cout << v << " ";
	cout << endl;	// 20 30 20 40 50 10 20

	// 20 -> 666
	cout << "替换后:" << endl;
	replace(vec.begin(), vec.end(), 20, 2000);
	for (auto v : vec)
		cout << v << " ";
	cout << endl;	// 666 30 666 40 50 10 666
}

int main()
{
	test01();
	system("pause");
	return 0;
}
replace_if(iterator beg, iterator end, _pred, newvalue);

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

#include 
#include 
#include 
using namespace std;

class ReplaceGreater30 {
public:
	bool operator()(int val) {
		return val >= 30;
	}

};

void test01()
{
	vector vec = { 20,30,20,40,50,10,20 };
	cout << "替换前:" << endl;
	for (auto v : vec)
		cout << v << " ";
	cout << endl;	// 20 30 20 40 50 10 20

	// ≥30 -> 666
	cout << "替换后:" << endl;
	replace_if(vec.begin(), vec.end(), ReplaceGreater30(), 3000);
	for (auto v : vec)
		cout << v << " ";
	cout << endl;	// 20 666 20 666 666 10 20
}

int main()
{
	test01();
	system("pause");
	return 0;
}
swap(container c1, container c2);

互换两个容器的元素,交换的容器要同种类型。

#include 
#include 
#include 
using namespace std;

void test01()
{

	vector vec1 = { 0,1,2,3,4,5 };
	vector vec2 = { 6,7,8,9,10,11 };

	cout << "交换前: " << endl;
	for (auto v : vec1)
		cout << v << " ";
	cout << endl;			// 0 1 2 3 4 5
	for (auto v : vec2)
		cout << v << " ";
	cout << endl;			// 6 7 8 9 10 11

	cout << "交换后: " << endl;
	swap(vec1, vec2);
	for (auto v : vec1)
		cout << v << " ";
	cout << endl;			// 6 7 8 9 10 11
	for (auto v : vec2)
		cout << v << " ";
	cout << endl;			// 0 1 2 3 4 5

}

int main()
{
	test01();
	system("pause");
	return 0;
}
常用算术生成算法

算术生成算法属于小型算法,使用时包含的头文件为 #include

accumulate(iterator beg, iterator end, value);

计算区间内容器元素累计总和。

fill(iterator beg, iterator end, value);

向容器中填充元素。

#include 
#include 
#include 
using namespace std;

void test01()
{
	vector vec;
	vec.reserve(101);
	for (int i = 0; i <= 100; i++) {
		vec.push_back(i);
	}
	int total = accumulate(vec.begin(), vec.end(), 0);
	cout << "total = " << total << endl;	// 5050
}

void test02()
{
	vector vec;
	vec.resize(5);
	// 填充vector有数据的部分 size
	fill(vec.begin(), vec.end(), 100);
	for (auto v : vec)
		cout << v << " ";
	cout << endl;	// 100 100 100 100 100
}

int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}
常用集合算法 set_intersection(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);

求两个容器的交集。

  • 求交集的两个集合必须是有序序列
  • 目标容器开辟空间为两个容器容量中的较小值
  • set_intersection返回交集中最后一个元素的下一个位置
set_union(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);

求两个集合的并集。

  • 求并集的两个集合必须的有序序列
  • 目标容器开辟空间为两个容器容量之和
  • set_intersection返回并集中最后一个元素的下一个位置
set_difference(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);

求两个容器的差集。

  • 求差集的两个集合必须是有序序列
  • 目标容器开辟空间为两个容器容量中的较大值
  • set_difference返回差集中最后一个元素的下一个位置
#include 
#include 
#include 
using namespace std;

void test01()
{
	vector vec1 = { 1,2,3,100,200,300 };
	vector vec2 = { 4,5,6,100,200,300 };

	vector vTarget;
	// 目标容器开辟空间为两个容器容量中的较小值
	vTarget.resize(min(vec1.size(), vec2.size()));

	// 返回目标容器结果的“尾后迭代器”
	vector::iterator itEnd =
		set_intersection(vec1.begin(), vec1.end(), vec2.begin(), vec2.end(), vTarget.begin());

	for (auto it = vTarget.begin(); it != itEnd; ++it)
		cout << *it << " ";
	cout << endl;	// 100 200 300

	for (auto v : vTarget)
		cout << v << " ";
	cout << endl;	// 100 200 300 0 0 0
}

void test02()
{
	vector vec1 = { 1,2,3,100,200,300 };
	vector vec2 = { 4,5,6,100,200,300 };

	vector vTarget;
	// 目标容器开辟空间为两个容器容量之和
	vTarget.resize(vec1.size() + vec2.size());

	// 返回目标容器结果的“尾后迭代器”
	vector::iterator itEnd =
		set_union(vec1.begin(), vec1.end(), vec2.begin(), vec2.end(), vTarget.begin());

	for (auto it = vTarget.begin(); it != itEnd; ++it)
		cout << *it << " ";
	cout << endl;	// 1 2 3 4 5 6 100 200 300

	for (auto v : vTarget)
		cout << v << " ";
	cout << endl;	//1 2 3 4 5 6 100 200 300 0 0 0
}

void test03()
{
	vector vec1 = { 1,2,3,100,200,300 };
	vector vec2 = { 4,5,6,100,200,300 };

	vector vTarget;
	// 目标容器开辟空间为两个容器容量中的较大值
	vTarget.resize(max(vec1.size(), vec2.size()));

	cout << "v1与v2的差集为: " << endl;
	vector::iterator itEnd =
		set_difference(vec1.begin(), vec1.end(), vec2.begin(), vec2.end(), vTarget.begin());
	for (auto it = vTarget.begin(); it != itEnd; ++it)
		cout << *it << " ";
	cout << endl;	// 1 2 3

	cout << "v2与v1的差集为: " << endl;
	itEnd = set_difference(vec2.begin(), vec2.end(), vec1.begin(), vec1.end(), vTarget.begin());
	for (auto it = vTarget.begin(); it != itEnd; ++it)
		cout << *it << " ";
	cout << endl;	// 4 5 6
}
int main()
{
	test01();
	test02();
	test03();
	system("pause");
	return 0;
}

参考:https://www.bilibili.com/video/BV1et411b73Z

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/849228.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号