10.排序及相关操作

本文介绍C++标准库中的排序算法及其应用案例,包括排序、查找、堆操作等,通过具体代码示例展示如何使用这些算法解决实际问题。

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

序号

功能

函数名称

说明

1

排序

Sort

以很好的平均效率排序

stable_sort

排序,并维持相同元素的原有顺序

partial_sort

将序列的前一部分排好序

partial_sort_copy

复制的同时将序列的前一部分排好序

2

第n个元素

nth_element

将第n各元素放到它的正确位置

3

二分检索

lower_bound

找到大于等于某值的第一次出现

upper_bound

找到大于某值的第一次出现

equal_range

找到(在不破坏顺序的前提下)可插入给定值的最大范围

binary_search

在有序序列中确定给定元素是否存在

4

归并

Merge

归并两个有序序列

inplace_merge

归并两个接续的有序序列

5

序结构上的集合操作

Includes

一序列为另一序列的子序列时为真

set_union

构造两个集合的有序并集

set_intersection

构造两个集合的有序交集

set_difference

构造两个集合的有序差集

set_symmetric_difference

构造两个集合的有序对称差集(并-交)

6

堆操作

make_heap

从序列构造堆

pop_heap

从堆中弹出元素

push_heap

向堆中加入元素

sort_heap

给堆排序

7

最大和最小

min

两个值中较小的

max

两个值中较大的

min_element

序列中的最小元素

max_element

序列中的最大元素

8

词典比较

lexicographical_compare

两个序列按字典序的第一个在前

9

排列生成器

next_permutation

按字典序的下一个排列

prev_permutation

按字典序的前一个排列

10

数值算法

accumulate

累积和

inner_product

内积

partial_sum

局部求和

adjacent_difference

临接差


1、排序

#include <iostream>
#include <algorithm>
#include<functional>
#include <vector>
#include<list>
#include<iterator>
using namespace std;
int main(int argc, char* argv[])
{
	int a[] = { 1, 8, 6, 10, 4 };
	vector<int> v(a, a + 5);
	sort(v.begin(), v.end());
	cout << "升序排序结果是: ";
	copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));//1 4 6 8 10


	cout << endl << "降序排列:";
	sort(v.begin(), v.end(), greater<int>());
	copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
	cout << endl;


	int b[] = { 10, 1, 3, 9, 7, 6, 2, 4, 5, 8 };
	vector<int> v1(b, b + 10);
	partial_sort(v1.begin(), v1.begin() + 4, v1.end());
	cout << "partial_sort后(前4个元素按升序排列):";
	copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
	cout << endl;


	int c[] = { 10, 1, 3, 9, 7, 6, 2, 4, 5, 8 };
	list<int> l(c, c + 10);
	//sort(l.begin(), l.end()); //这一行是错误的
	l.sort();	//这一行正确
	copy(l.begin(), l.end(), ostream_iterator<int>(cout, " "));//1 2 3 4 5 6 7 8 9 10

	return 0;
}


2、第n个元素

#include <iostream>
#include <algorithm>
#include<functional>
#include <vector>
#include<iterator>
using namespace std;

class Student
{
public:
	int NO;
	string name;
	int grade;
	Student(int NO, string name, int grade) :NO(NO), name(name), grade(grade){}
	bool operator>(const Student&s)const
	{
		return grade > s.grade;
	}
};
int main()
{
	vector<Student>v;
	Student s1(101, "aaa", 90);
	Student s2(102, "aaa", 80);
	Student s3(103, "aaa", 96);
	Student s4(104, "aaa", 94);
	Student s5(105, "aaa", 25);
	Student s6(106, "aaa", 58);
	Student s7(107, "aaa", 54);
	Student s8(108, "aaa", 45);
	v.push_back(s1); v.push_back(s2);
	v.push_back(s3); v.push_back(s4);
	v.push_back(s5); v.push_back(s6);
	v.push_back(s7); v.push_back(s8);
       //找第三个成绩最好的
	nth_element(v.begin(), v.begin() + 2, v.end(), greater<Student>());
	vector<Student>::iterator ix = v.begin() + 2;
	cout << (*ix).NO << " " << (*ix).grade << endl;


}

3、二分检索

#include <iostream>  
#include <algorithm>   
#include<functional>   
#include<list>  
#include<iterator>
using namespace std;

int main()
{
	int a[] = { 1,2,2,3,3,3,4, 4, 4, 4 };
	list<int>l1(a, a + 10);

	bool flag = binary_search(l1.begin(), l1.end(), 5);
	cout << "有5元素么:" << flag << endl;

	//大于等于2第一个元素出现的迭代器
	list<int>::iterator first = lower_bound(l1.begin(), l1.end(), 2);
	if (first != l1.end())
	{
		cout << distance(l1.begin(), first) << endl;
	}

	//大于3第一次出现的迭代器位置
	list<int>::iterator last = upper_bound(l1.begin(), l1.end(), 3);
	if (last != l1.end())
		cout << distance(l1.begin(), --last) << endl;

	pair<list<int>::iterator, list<int>::iterator>p = equal_range(l1.begin(), l1.end(), 4);
	if (p.first != p.second)
	{
		int size = distance(p.first, p.second);
		cout << size << endl;
	}
}

4、归并


5、序结构上的集合操作

序结构上的集合操作

Includes

一序列为另一序列的子序列时为真

set_union

构造两个集合的有序并集

set_intersection

构造两个集合的有序交集

set_difference

构造两个集合的有序差集

set_symmetric_difference

构造两个集合的有序对称差集(并-交)


6、堆操作

#include <iostream>
#include <algorithm>
#include<functional>
#include <vector>
#include<list>
#include<iterator>
using namespace std;

int main()
{
	int a[] = { 1, 4, 2, 10, 6, 5, 9, 7, 8, 3 };
	vector<int>v1(a, a + 10);
	//greater形成最小堆
	make_heap(v1.begin(), v1.end(), greater<int>());
	cout << "最小堆:";
	copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
	cout << endl;//1 3 2 7 4 5 9 10 8 6

	vector<int>v2(a, a + 10);
	make_heap(v2.begin(), v2.end(), less<int>());
	cout << "最大堆:";
	copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
	cout << endl; //10 8 9 7 6 5 2 1 4 3

	pop_heap(v2.begin(), v2.end());
	cout << *(v2.end() - 1) << endl;
	pop_heap(v2.begin(), v2.end()-1);
	cout << *(v2.end() - 2) << endl;
}

7、最大和最小

#include <iostream>
#include <algorithm>
using namespace std;
void main()
{
	int a = _MIN(10,20);
	int b = _MAX(10,20);
	cout << "min(10,20):" << a << endl;
	cout << "max(10,20):" << b << endl;
	int c[]={1,10,5,7,9};
	cout << "原始c[]:"; 
	copy(c, c+5, ostream_iterator<int>(cout, "\t"));//1 10 5 7 9
	cout << endl;
	int *it_min = min_element(c, c+5);
	int *it_max = max_element(c, c+5);
	cout << "c[]最小值:" << *it_min << endl;//1
	cout << "c[]最大值:" << *it_max << endl;//10
}

8、词典比较

int a[] = { 1, 2, 3,5};
	int b[] = { 1, 2,4};
	bool bRet = lexicographical_compare(a, a + 3, b, b + 3);
	cout << (bRet == 1 ? "a[]<b[]" : "a[]>b[]") << endl;  //a[]>b[]

9、全排列

#include <iostream>
#include <algorithm>
#include<iterator>
using namespace std;
void main()
{
	int a[] = { 0,0,1,1,3,2};
	sort(a, a + 6);
	if (a[0] == 0)
	{
		int *it = upper_bound(a, a + 6, 0);
		swap(a[0], *it);
	}
	int count = 0;
	do
	{
		count++;
		copy(a, a + 6, ostream_iterator<int>(cout, "\t"));
		cout << endl;
	} while (next_permutation(a, a + 6));

	cout << count << endl;
}

10、数值算法

【例10.18】计算1+(1*2)+(1*2*3)+……前10项和。

#include <iostream>
#include <numeric>
#include <functional>
#include <vector>
#include <iterator>
using namespace std;
void main ()
{
	vector<int> v(10);
	for(int i=0; i<10; i++)
	{
		v[i] = i+1;
	}
	vector<int> mid(10);
	partial_sum(v.begin(), v.end(), mid.begin(), multiplies<int>());
	int sum = accumulate(mid.begin(), mid.end(),0);
	cout<< "1+(1*2)+(1*2*3)+......前10项和是:" << sum << endl;
}


【例10.18】计算1+(1*2)+(1*2*3)+……前10项和。

#include <iostream>
#include <numeric>
#include <functional>
#include <vector>
#include <iterator>
using namespace std;
void main ()
{
	vector<int> v(10);
	for(int i=0; i<10; i++)
	{
		v[i] = i+1;
	}
	vector<int> mid(10);
	partial_sum(v.begin(), v.end(), mid.begin(), multiplies<int>());
	int sum = accumulate(mid.begin(), mid.end(),0);
	cout<< "1+(1*2)+(1*2*3)+......前10项和是:" << sum << endl;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值