STL学习(19):排序及相关操作

  1. 排序
  2. 第n个元素
  3. 二分检索
  4. 归并
  5. 序结构上的集合操作
  6. 堆操作
  7. 最大和最小
  8. 词典比较
  9. 排列生成器
  10. 数值算法
  11. 自定义STL函数

主要函数如下所示。

序号

功能

函数名称

说明

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

堆操作

   

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

临接差

排序

主要函数

①sort 原形:

template<class RanIt>
void sort(RanIt first, RanIt last);
template<class RanIt, class Pred>
void sort(RanIt first, RanIt last, Pred pr); 

第一个模板函数[first,last)间迭代器指示的元素数据按升序排列,第二个模板函数定义了比较函数pr(x,y)代替了operator<(x,y), 功能是相似的,属于不稳定排序。

②stable_sort 原型:

template<class RanIt>
void stable_sort(RanIt first, RanIt last);
template<class RanIt, class Pred>
void stable_sort(RanIt first, RanIt last, Pred pr);

第一个sort函数[first,last)间迭代器指示的元素数据按升序排列,第二个sort函数定义了比较函数pr(x,y)代替了operator<(x,y), 功能是相似的。与sort函数相比,和它的名字一样,属于稳定排序。

③partial_sort 原型:

template<class RanIt>
void partial_sort(RanIt first, RanIt middle, RanIt last);
template<class RanIt, class Pred>
void partial_sort(RanIt first, RanIt middle, RanIt last, Pred pr); 

该函数实现了局部元素排序功能。对[first, last)间的元素排序结束后,仅前middle-first-1个元素是必须按要求排好序的,其它元素不一定是排好序的。即:对任意N[0,middle-first],M(N,last-first),都有*(first+N)<*(first+M)。第二个函数与第一个函数相比定义了比较函数pr(x,y)代替了operator<, 功能是相似的。

④partial_sort_copy 原型:

template<class InIt, class RanIt>
RanIt partial_sort_copy(InIt first1, InIt last1,RanIt first2, RanIt last2);   
template<class InIt, class RanIt, class Pred>
RanIt partial_sort_copy(InIt first1, InIt last1,RanIt first2, RanIt last2, Pred pr); 

该函数功能是: 与partial_sort相比有两点主要不同:(1)排序结果可以输出到另外一个容器(当然也可自身容器);(2)partial_sort函数中直接给出了middle值,而该函数middle值是计算出来的。middle=min(last1-first1,last2-first2)+first1。之后:对任意N[0,middle-first1],M(N,last1-first1),都有*(first2+N)<*(first1+M)。第二个函数与第一个函数相比定义了比较函数pr(x,y)代替了operator<, 功能是相似的。

对学生成绩进行升序排列

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
class Student
{
public:
	int NO;	//学号
	string name;
	int grade; //成绩
	Student(int NO, string name, int grade)
	{
		this->NO = NO;
		this->name = name;
		this->grade = grade;
	}
	bool operator<(const Student &s) const
	{
		return grade < s.grade;
	}
};
ostream& operator << (ostream& os, const Student& s)
{
	os << s.NO << "\t" << s.name << "\t" << s.grade;
	return os;
}
int main(int argc, char* argv[])
{
	Student s1(101,"张三", 90);
	Student s2(102,"李司", 80);
	Student s3(103, "王五", 85);
	Student s4(103, "赵六", 65);
	vector<Student> v;
	v.push_back(s1);
	v.push_back(s2);
	v.push_back(s3);
	v.push_back(s4);
	sort(v.begin(), v.end());
	cout << "升序排序结果是: " << endl;
	cout << "学号\t" << "姓名\t" << "成绩" << endl;
	copy(v.begin(), v.end(), ostream_iterator<Student>(cout, "\n"));
	return 0;
}

利用partial_sort取整形向量最小的4个元素。

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void main()
{
	int a[] = {10,1,3,9,7,6,2,4,5,8};
	vector<int> v(a, a+10);
	cout << "原始向量数据:";
	copy(v.begin(), v.end(), ostream_iterator<int>(cout, "\t"));
	cout << endl;
	partial_sort(v.begin(), v.begin()+4, v.end()) ;
	cout << "partial_sort后(前4个元素按升序排列):" ;
	copy(v.begin(), v.end(), ostream_iterator<int>(cout, "\t"));
	cout << endl;
}

list容器排序问题。

#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
void main()
{
	int a[] = {10,1,3,9,7,6,2,4,5,8};
	list<int> l(a, a+10);
	//sort(l.begin(), l.end()); //这一行是错误的
	l.sort();	//这一行正确
	copy(l.begin(), l.end(), ostream_iterator<int>(cout, "\t"));//1 2 3 4 5 6 7 8 9 10
}

注释行的程序是错误的,说明list容器不能用sort通用排序算法。这是由于sort需要的是随机迭代器,方便排序算法中的数据交换,而list提供的仅是双向迭代器。因此要想排序,只能用list类本身提供的sort函数,它有两种形式,已经在第6章6.3节list容器中简单讲过。如果要想让示例中元素降幂排列,如下调用就可以了:l.sort(greater<int>())。当然也可以定义自定义二元函数。

猜你喜欢

转载自blog.csdn.net/QQ2558030393/article/details/93479643
今日推荐