STL:排序
来源:互联网 发布:网络电视怎么放音乐 编辑:程序博客网 时间:2024/05/18 08:45
前言
区间范围[a, b)全局排序:给定区间,全部排序。有sort, stable_sort。
局部排序:给定区间,只需排除前几个,或后几个。比如:一个班级100名学生,只输出前10名同学,并且有序。或者倒数10名。有partial_sort, partial_stable_sort。
测试文本内容:
1 3 4 2 5 8 6 7 9 10
测试1:partial_sort
#include <stdio.h>#include <iostream>#include <algorithm>using namespace std;int main(){#ifdef ONLINE_JUDGE#else freopen("E:\\in.txt", "r", stdin);#endifint buf[100];for(int i=0; i<10; i++){scanf("%d", &buf[i]);}partial_sort(buf, buf+3, buf+10); //前3个有序for(int i=0; i<10; i++){printf("%d ", buf[i]);}cout << endl;return 0;}
结果:
10 1 8 3 7 2 5 6 9 4 //排序前1 2 3 10 8 7 5 6 9 4 //排序后
测试2:nth_element():
只求第几小,或第几大。比如,我要求第3小,那么buf[3-1]这个位置上就应当是第3小的值,其他不定。
代码与上面相同,仅仅把partial_sort 换成nth_element.
nth_element(buf, buf+3, buf+10);
10 1 8 3 7 2 5 6 9 4 //前3 1 2 4 7 8 5 6 9 10 //后
我们求的是倒数第4(buf+3是第4个位置)小,理论上,第四个位置应为4.实际也是。
0 0
- STL排序
- STL 排序
- STL排序
- stl排序
- STL:排序
- STL---排序
- STL 排序(Sort)
- STL对字符串排序
- STL 排序(Sort)
- STL关于排序
- STL排序算法
- STL排序算法
- STL排序比
- STL 排序(Sort)
- STL中的sort排序
- STL SET 类 排序
- STL 稳定排序
- 详细解说STL 排序
- 传统百货扎堆试水O2O
- 黑马程序员01_多线程
- Gas Station
- HDU 4135容斥原理求互质的个数
- 二分uva501
- STL:排序
- PHP编码规范(2)
- 深入浅出MFC系列 第11章和12章的学习笔记
- VC基本操作四十条
- GTD 实战与总结
- UVA1513--Movie collection--线段树
- 假币的型号
- HDU 4745 Two Rabbits (最长回文变形)
- emulator-arm.exe已停止工作