C++STL算法

本文详细介绍了STL中的一系列算法,包括基本查找(如find、find_if等)、统计查找、有序查找、排序算法(如merge、inplace_merge、sort等)、删除替换算法、算术算法、生成变异算法、关系算法以及集合算法。通过实例展示了如何使用这些算法进行容器操作,如查找、排序、删除、转换和比较元素,以及进行并集、交集、差集和对操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

STL查找算法

1.基本查找

(1)find:区间查找

(2)find_if:条件查找

(3)find_firt_of: 查找区间第一次出现值

(4)adjacent_find: 查找第一次重复的数

(5)search:子序列查找

(6)search_n: 子序列查找出现次数

2.统计查找

(1)count: 区间统计

(2)count_if: 条件统计个数

(3)equal:比较

3.有序查找

(1)binary_search:二分查找

(2)upper_bound: 查找最后一个大于查找的值

(3)lower_bound: 大于等于查找的值

(4)equal_range:区间比较---有序序列

#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
using namespace std;
int main()
{
	//1.find查找
	int array[] = { 0,1, 2, 3, 5, 5, 5, 7, 7, 9 };
	vector<int> vecdata;
	vecdata.assign(array, array + 10);
	auto temp = find(vecdata.begin(), vecdata.end(), 1);
	//2.find_if条件查找
	auto temp1 = find_if(vecdata.begin(), vecdata.end(), [](int a)->bool{return a > 3; });
	//3.find_first_of查找第一个符合数据
	int array1[] = { 99, 2, 1, 4 };
	auto temp2 = find_first_of(vecdata.begin(), vecdata.end(), array1, array1 + 4);//1
	//4.adjacent_find查找重复的元素
	auto temp3 = adjacent_find(vecdata.begin(), vecdata.end());
	//5.search找子串
	int array2[] = { 5,5,5 };
	auto temp4 = search(vecdata.begin(), vecdata.end(), array2, array2 + 2);//5
	//6.search_n查找重复的次数
	auto temp5 = search_n(vecdata.begin(), vecdata.end(), 3, 5);//5
	temp5 = search_n(vecdata.begin(), vecdata.end(), 3, 5, [](int a, int b)->bool{return a > b ; });//7
	//7.count查找统计的次数
	count(vecdata.begin(), vecdata.end(), 5);
	//8.count_if条件统计次数
	count_if(vecdata.begin(), vecdata.end(), [](int a){return a > 5; });
	//9.equal比较
	int array3[] = { 1, 2, 3, 5, 5, 5, 7, 7, 9, 0 };
	equal(vecdata.begin(), vecdata.end(),array3);
	//10.binary_search二分查找(数据有序)
	binary_search(vecdata.begin(), vecdata.end(), 9);
	//11.upper_bound查找大于的(数据有序)
	auto temp6 = upper_bound(vecdata.begin(), vecdata.end(), 5);
	//12.lower_bound查找大于等于的(数据有序)
	auto temp7 = lower_bound(vecdata.begin(), vecdata.end(), 5);
	//13.equal_range包含大于等于和大于
	auto temp8 = equal_range(vecdata.begin(), vecdata.end(), 5);
	cout << *temp8.first << "\t" << *temp8.second;
	return 0;
}

STL排序通用算法

1.merge: 归并排序,存于新容器

2.inplace_merge: 归并排序,覆盖原区间

3.sort: 排序,更改原容器顺序

4.stable_sort: 排序,保存原容器数据顺序

5.nth_element: 关键字排序

6.partition:范围排序

7.partial_sort:范围排序

8.partial_sort_copy:范围排序外加复制操作

9.stable_partition: 范围排序,保存原容器顺序

10.random_shuffle: 随机排序

11.reverse:逆序原容器

12.reverse_copy: 逆序容器保存到新容器

13.rotate:移动元素到容器末尾

14.rotate_copy:移动元素到新容器

#include <iostream>
#include <algorithm>
#include <functional>
#include <time.h>
#include <vector>
using namespace std;
int main()
{
	//1.merge归并排序,存于新容器
	vector<int> vec_merge = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	vector<int> vecdata1(18);
	merge(vec_merge.begin(), vec_merge.end(), vec_merge.begin(), vec_merge.end(), vecdata1.begin());//112233445566778899
	//2.inplace_merge归并排序,存于原容器
	vector<int> vec_inp_merge = { 1, 3, 5, 7, 9, 2, 4, 6, 8 };
	inplace_merge(vec_inp_merge.begin(), vec_inp_merge.begin() + 5, vec_inp_merge.begin() + 9);//123456789
	//3.sort默认从小排序
	vector<int> vecdata = { 2, 3, 5, 1, 2, 5, 6, 7, 8, 9, 3 };
	sort(vecdata.begin(), vecdata.end(), less<int>());//12233556789
	//4.stable_sort不改变相对位置排序
	vector<double> vecdou = { 1.33, 2.22, 1.22, 3.11, 3.22, 3.33 };
	stable_sort(vecdou.begin(), vecdou.end(), [](double a, double b){return int(a) < int(b); });//1.33 1.22 2.22 3.11 3.22 3.33
	//5.nth_element关键字排序
	vector<int> vec_nth_ele = { 1, 3, 5, 7, 9, 2, 4, 6, 8 };
	nth_element(vec_nth_ele.begin(), vec_nth_ele.begin() + 3, vec_nth_ele.end());
	//6.partition分类处理,满足存放于左边,存于原容器
	vector<int> vec_partition = { 1, 3, 5, 7, 9, 2, 4, 6, 8 };
	partition(vec_partition.begin(), vec_partition.end(), [](int a){return a>5; });//8 6 9 7 5 2 4 3 1
	//7.partial_sort分类处理,满足存放于左边,存于原容器
	vector<int> vec_par_sor = { 1,3,5,7,9,2,4,6,8 };
	partial_sort(vec_par_sor.begin(), vec_par_sor.begin() +4, vec_par_sor.end());//1 2 3 4 9 7 5 6 8
	//8.partial_sort_copy局部排序,结果另存
	vector<int> vec_par_sor_cop(4);
	partial_sort_copy(vec_par_sor.begin(), vec_par_sor.begin() + 4, vec_par_sor_cop.begin(), vec_par_sor_cop.end());//1 2 3 4
	//9.stable_partition保持原容器的相对顺序排序
	vector<double> vec_sta_par = { 1.33, 2.22, 1.22, 3.11, 3.22, 3.33 };
	stable_partition(vec_sta_par.begin(), vec_sta_par.end(), [](double a){return a < 2.00; });//1.33 1.22 2.22 3.11 3.22 3.33
	//10.random_shuffle乱序算法,每次结果不同
	vector<int> vec_rand{ 1, 3, 5, 7, 9, 2, 4, 6, 8 };
	srand((unsigned int)time(nullptr));
	random_shuffle(vec_rand.begin(), vec_rand.end());
	//11.reverse逆序反转
	vector<int> vec_rev{ 1, 3, 5, 7, 9, 2, 4, 6, 8 };
	reverse(vec_rev.begin(), vec_rev.end());//8 6 4 2 9 7 5 3 1
	//12.reverse_copy反转另存
	vector<int> vec_rev_cop(9);
	reverse_copy(vec_rev.begin(), vec_rev.end(), vec_rev_cop.begin());//1 3 5 7 9 2 4 6 8
	//13.rotate移位操作
	vector<int> move = { 1, 2, 3, 4, 5, 6 };
	rotate(move.begin(), move.begin() + 2, move.end());//3 4 5 6 1 2
	//14.rotate_copy移位拷贝
	vector<int> move_res(6);
	rotate_copy(move.begin(), move.begin() + 2, move.end(), move_res.begin());//5 6 1 2 3 4
	return 0;
}

STL删除替换算法

1.copy: 拷贝函数

2.copy_backward: 逆序拷贝

3.iter_swap: 交换

4.remove: 删除

5.remove_copy: 删除元素复制到新容器

6.remove_if:条件删除

7.remove_copy_if:条件删除拷贝到新容器

8.replace:替换

9.replace_copy: 替换,结果放到新容器

10/replace_if: 条件替换

11.replace_copy_if:条件替换,结果另存

12.swap: 交换

13.swap_range:区间交换

14.unique:去重

15.unique_copy:去重,结果另存

#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;
int main()
{
	//1.copy正向拷贝
	vector<int> vec = { 1, 2, 3, 4, 5, 6 };
	vector<int> nvec(vec.size());
	copy(vec.begin(), vec.end(), nvec.begin());//1 2 3 4 5 6
	//2.copy_backward逆向拷贝,从最后开始赋值
	vector<int> rvec(vec.size());
	copy_backward(vec.begin(), vec.end(), rvec.end());//1 2 3 4 5 6
	//3.remove删除--->伪删除
	auto it = remove(vec.begin(), vec.end(), 3);//size:6  result:1 2 4 5 6 6
	//4.remove_copy伪删除结果另存
	vector<int> result(vec.size() - 1);
	remove_copy(vec.begin(), vec.end(), result.begin(), 2);//1 4 5 6 6
	//5.remove_if条件删除 
	vector<int> score = { 98, 45, 65, 77, 45, 43 };
	remove_if(score.begin(), score.end(), [](int x) {return x < 60; });//98 65 77 77 45 43
	//6.remove_copy_if条件删除结果另存
	vector<int> ok(count_if(score.begin(), score.end(), [](int x) {return x >= 60; }));//4
	remove_copy_if(score.begin(), score.end(), ok.begin(), [](int x) {return x < 60; });//98 65 77 77
	//7.replace替换算法(修改) 
	vector<int> rep = { 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 1, 2, 3 };
	replace(rep.begin(), rep.end(), 1, 100);//100 2 3 4 5 6 7 100 2 3 100 2 3
	//8.replace_copy替换后结果另存
	vector<int> repc(rep.size());
	replace_copy(rep.begin(), rep.end(), repc.begin(), 100, 0);//0 2 3 4 5 6 7 0 2 3 0 2 3
	//9.replace_if条件替换
	replace_if(rep.begin(), rep.end(), [](int x) {return x < 6; }, 0);//100 0 0 0 0 6 7 100 0 0 100 0 0
	//10.replace_copy_if条件替换,结果另存
	vector<int> repif(rep.size());
	replace_copy_if(rep.begin(), rep.end(), repif.begin(), [](int x) {return x == 0; }, 1);//100 1 1 1 1 6 7 100 1 1 100 1 1
	//11.iter_swap交换第一个元素和最后一个元素 
	vector<int> test = { 11, 2, 3, 4, 5, 6, 7, 8, 9 };
	iter_swap(test.begin(), test.end() - 1); //11 2 3 4 5 6 7 8 9
	//12.swap 不需要两个容器长度一样
	vector<int> test2 = { 2, 2 };
	swap(test, test2);//test:{ 2,2 }  test2:{ 11,2,3,4,5,6,7,8,9 }
	//13.swap_range区间交换  
	swap_ranges(test2.begin(), test2.begin() + 1, test.begin()); //test2:{ 2, 2, 3, 4, 5, 6, 7, 8, 9 }
	//14.unique去重(连续相同保留一个)  想要去重可以先排序,在调用
	vector<int> uniq = { 1, 1, 3, 4, 4, 3, 3, 2, 2, 2, 4, 4, 4 };
	//unique(uniq.begin(), uniq.end());
	//15.去重另存
	vector<int> uniqr(6);
	unique_copy(uniq.begin(), uniq.end(), uniqr.begin());//1 3 4 3 2 4
	return 0;
}

STL排列组合算法

1.next_permutation:下一个排序序列的组合

2.prev_permutation:上一个排序序列的组合

	array<int, 3> arrdata = { 1, 2, 3 };
	next_permutation(arrdata.begin(), arrdata.end());//123的下一个排列组合132	
	prev_permutation(arrdata.begin(), arrdata.end());//132的上一个排列组合123

STL算术算法

1.accumulate:区间求和

2.partial_sum:相邻元素的和

3.inner_product:序列内积运算

4.adjacent_difference:相邻元素的差

#include <iostream>
#include <algorithm>
#include <iterator>
#include <numeric>
#include <vector>
#include <functional>
using namespace std;
int main()
{
	vector<int> vecsum = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	//1.accumulate求和
	int sum;
	sum = accumulate(vecsum.begin(), vecsum.end(), 0);//45
	//2.partial_sum逐步求和
	vector<int> par_sum(vecsum.size());
	partial_sum(vecsum.begin(), vecsum.end(), par_sum.begin());//1 3 6 10 15 21 28 36 45
	//3.inner_product求内积运算:举证乘法
	vector<int> first = { 1, 2, 3, 4 };
	vector<int> second = { 1, 2, 3, 4 };
	inner_product(first.begin(), first.end(), second.begin(), 0);//30
	//4.adjacent_difference求差值
	vector<int> test = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	vector<int> res(9);
	adjacent_difference(test.begin(), test.end(), res.begin());//111111111
	return 0;
}

STL生成变异算法

1.for_each:迭代访问

2.fill:填充方式初始容器

3.fill_n:指定长度填充容器

4.generate_n:填充前n个位置

5.transform:一元转换和二元转换

#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;
int main()
{
	vector<int> vecfe = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	//1.for_each迭代器访问数据
	for_each(vecfe.begin(), vecfe.end(), [](int x){cout << x << " "; });//1 2 3 4 5 6 7 8 9
	//2.fill填充
	fill(vecfe.begin(), vecfe.end(), 10);//10 10 10 10 10 10 10 10 10
	//3.fill_n填充制定长度数据
	fill_n(vecfe.begin()+2, 2, 0);//10 10 0 0 10 10 10 10 10
	//4.generate_n填充前n个数据
	generate_n(vecfe.begin(), 2, []{return 0; });//0 0 0 0 10 10 10 10 10
	generate(vecfe.begin(),vecfe.end(), []{return 0; });//0 0 0 0 0 0 0 0 0
	//5.transform转换并另存
	vector<int> temp(9);
	transform(vecfe.begin(), vecfe.end(), temp.begin(), [](int data){return data + 1; });//1 1 1 1 1 1 1 1 1 
	return 0;
}

STL关系算法

1.equal:两容器元素是否都相同

2.includes:是否是包含关系

3.lexicographical_compare:比较两个序列

4.max:求最大值

5.max_element:返回最大值的iterator

6.min:求最小值

7.min_element:求最小值的iterator

8.mismatch:找到第一个不同的位置

#include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>
using namespace std;
int main()
{
	//1.equal比较
	vector<int> one = { 1, 2, 3, 4, 5, 6, 7 };
	vector<int> two = { 1, 2, 3, 4, 5, 7, 6 };
	cout <<boolalpha<<  equal(one.begin(), one.end(), two.begin()) << endl;//false
	//2.includes包含关系要是有序
	vector<int> thrid = { 1, 2, 3 };
	cout << boolalpha<< includes(one.begin(), one.end(),thrid.begin(), thrid.end()) << endl;//ture
	//3.lexicographical_compare比较 第一个小于第二个就返回true
	cout << boolalpha << lexicographical_compare(one.begin(), one.end(),two.begin(), two.end()) << endl;//ture
	//4.max求最大值
	cout << "max:" << max(1, 3) << endl;
	cout << "Max:" << *max_element(one.begin(), one.end()) << endl;
	//5.min求最小值
	cout << "min:" << min(1, 2) << endl;
	cout << "Min:" << *min_element(one.begin(), one.end()) << endl;
	//6.mismatch找第一个不同地方
	cout << *mismatch(one.begin(), one.end(), two.begin()).first << endl;
	cout << *mismatch(one.begin(), one.end(), two.begin()).second << endl;
	//first: 第一容器不同的元素的位置
	//second: 第二个容器不同的元素的位置
	return 0;
}

STL集合算法

1.set_union:并集

2.set_intersection:交集

3.set_difference:差集

4.set_symmetric_difference:对称差集

#include <iostream>
#include <algorithm>
#include <iterator>
#include <functional>
#include <vector>
using namespace std;
int main()
{
	vector<int> one = { 1, 2, 3, 4, 5, 6 };
	vector<int> two = { 4, 5, 6, 7, 8, 9 };
	vector<int> result(one.size() + two.size());
	//1.并集
	set_union(one.begin(), one.end(), two.begin(), two.end(),result.begin());
	for_each(result.begin(), result.end(),[](int x) {if (x != 0) { cout << x << "\t"; }});
	cout << endl;
	//2.交集
	set_intersection(one.begin(), one.end(), two.begin(), two.end(),ostream_iterator<int>(cout, "\t"));
	cout << endl;
	//3.求差集
	set_difference(one.begin(), one.end(), two.begin(), two.end(),ostream_iterator<int>(cout, "\t"));
	cout << endl;
	//4.对称差集
	set_symmetric_difference(one.begin(), one.end(), two.begin(), two.end(),ostream_iterator<int>(cout, "\t"));
	cout << endl;
	return 0;
}

STL对算法

1make_heap:生成一个堆

2.pop_heap:把要出堆的元素发放到容器后面,并没有真正删除,需要手动调用容器的删除函数删除

3.push_heap:入堆

4.sort_heap:堆排序,堆排序准则一定要和生成准则一致

#include <iostream>
#include <algorithm>
#include <string>
#include <functional>
#include <vector>
using namespace std;
int main()
{
	vector<int> data = { 1, 3, 19, 9, 4, 7 };
	make_heap(data.begin(), data.end());		//默认生成形式是大顶堆
	while (!data.empty())
	{
		pop_heap(data.begin(), data.end());  //要出去的元素放到最后面
		cout << data.back() << "\t";
		data.pop_back();				//真正的删除元素
	}
	vector<int> data3 = { 1, 3, 19, 9, 4, 7 };
	make_heap(data3.begin(), data3.end(), greater<int>());//生成小顶堆
	sort_heap(data3.begin(), data3.end(), greater<int>()); //堆排序准则一定要和生成准则一致
	for (auto v : data3)
	{
		cout << v << "\t";
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值