stl sort和qsort的使用
好不容易使用了下stl的qsort函数,顺便和sort函数一起总结下:
很多时候我们都需要用到排序。
例如:
1 #include <iostream>
#include <algorithm> using namespace std; int a[] = {, , , , };//对这个数组排序
int main()
{
sort(a, a + );
for (int i = ; i < ; i++)
cout<<a[i]<<endl;
return ;
}
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAcsAAACZCAIAAADPUYZYAAAgAElEQVR4nO2d+XcTV4Lv73/Sncw5ncw53en+LxrCYvO61ZPezpyZeS9vepmeN0sHMAnQMYTMZOR5s+R0CEk3gXQMhhAMchJCANkYCHgpy7ZsS960lWTtkjetpTM/1HZrVVXJwph8P+d7OLIo3br3qvTR1a2NvP3229/73vd+/94HU9H1R0trI8vro6H10fCmUcYim2OR0nikNB4t85mIVibYygRbmYhVJmJlH1vyRUtMdGM8vDYeWmNCa0x4dSK8OhFenYysT8U2/fHyTKI8u1KdS9bmUlwgzacRTDeCGTkBZ0nLmWs9qcZcqjGrlxlN/JpM00k2poRwiqxwk2J8VCYSchg+cUXGxYxRGeXDcqMsN0LlEZ+YTh5KiXIPo9xXenkgJaLIfQcJG+Yegjx1IT09Pd/41rfTxfKjxeLDpbWR5fURa4Ydi5aYaJmJlSdiFR9b8bGVSSmxki+26Ytu+KLrk9G1qej6VHRtKrrmj23MxDdnE6VAshJMVYOp2nymvpDlFrLcQraxKGah5czbTFA3GcOotK5V86wqspq5GTF+KtNSkopoLawQcUKOjpETHJPgxvloXCyHFTKqCe3okZgiurJ2locI8vSGvPnmm9/41rfXSrWHdgw7xhs2VmZiZV+s4mMrk/HKJFuZilen45XpeHk6XppmN6fZDT+74Wc3ZtiNGXZjLrEZSJaCycp8qrKQqS1ma0tZbinX4LOcayznG0vOknOYRaMYuN6urI0G2ro61o6LVaNjaThMZ0rKSmOSis8gE3QSjYlEgzFPXJFxg4yZhDXMKII81SEnTpz4xre+vV6u2zXseLTExMoTvGHjlcl4dSpRnU5U/YnqzEp5JlGeSZRnE6W5RGkuUQq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waSV8aSV0aSQVXNn70v/9xuysIAAA7lq6urmeffyG3Xrv0KHnpUbLvIZ+VvocrgcT6j/7mH0yOX3sq+Zo00zroDQCcc/To0WeefyG3Xu37KnHxq8TFB3ziFx/E5+Jrrr/5f+QJPE2izTyGNj5pfWhSn6/DOw5Au9i7dy9v2AtfxS884MNeeMD2PmBh2B1avl2etPoA8PRw5MiRZ557IbdevfAgfuE+H7b3Ptt7n51j11x/bWhY+te0yVlu9LrsPm+E7vJeJdrltY/Nyzd63GL5Xg3m7bJYVNPnrazUSn3MF9btBOJq5CIAAAFuSURBVLvtAuDp4ciRI8IY9kHiwoPEhQfxXjGqMazJh4r+L6NPmt3HVrBYjt1V2GpLK+VbaYuVQlrsZ/PVWWysg/UC8PTT1dUljGHvxy/cZ6UBrNEYlsYrQjRWolEtb1SO9Trrlm/RIFbWZVL5pu2yXr7Jk44LMSnHYv80XZHd8k36DYCnn0OHDj3z3AvZ9Spv1Y+oWDSslSetLGDx49eiQWzJy+ILHZe/5YWYlGOxf5quyG75VtoCwFPLwYMHn3nuO9m16kfD0T8pM8uu/vCv/p60YNh2fBqdleOlMC/fqKh2lP84+8GkfN366K5oa9dLqH4zbywAO5VXXnnlm899J7tWOT8UUmUmWvzBX/3GyB0mTrH7EpOidKEX1rWDUVFWVqGqifaxSSHOmqB90m4hVsox75+m5VjpZ/PFdNtlq70A7Dz+7u9+881vfTuzWj57e/Hs7QU5txb8kfz/+stfb3cFt4x2f5JhCgCAiv8Bc6psxyd+TIkAAAAASUVORK5CYII=" alt="" />
可以看到,sort默认是从小到达排序的,那如何自定义排序呢。
先看看sort的函数声明:
template <class RandomAccessIterator>
void sort (RandomAccessIterator first, RandomAccessIterator last); template <class RandomAccessIterator, class Compare>
void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);
sort函数将[first, last]区间按升序进行排序,排序是使用值类型的<运算符进行比较。第一个版本使用<来确定顺序,也就是从大到小,
第二个版本则使用自定义的比较对象或比较函数。
现在要对a[] = {, , , , };进行升序。
#include <iostream>
#include <algorithm> using namespace std; int a[] = {, , , , };
int b[] = {, , , , };
bool cmp(const int a, const int b)
{
return a > b;
}
class cmpClass
{
public:
bool operator () (const int a, const int b)
{
return a > b;
}
};
struct cmpStruct/**< 使用类似cmpClass */
{
bool operator () (const int a, const int b)/**< 自定义比较对象需要重载()才行 */
{
return a > b;
}
};
int main()
{
sort(a, a + , cmp);/**< 使用自定义函数 */
cout<<"a[]:";
for (int i = ; i < ; i++)
cout<<' '<<a[i];
cout<<endl;
cmpClass a;/**< 先声明个对象 */
sort(b, b + , a);/**< 使用比较对象 */
cout<<"b[]:";
for (int i = ; i < ; i++)
cout<<' '<<b[i];
cout<<endl;
return ;
}
运行结果:
a[]:
b[]:
对于自定义类型也可以使用sort,但必须提供比较函数。
例如
#include <iostream>
#include <algorithm> using namespace std;
struct point
{
int x, y;
bool operator < (const point &b)const/**<重载<号,可根据自己需要实现内部代码 */
{
if (x == b.x)
return y < b.y;
return x < b.x;
}
};
point p[] =
{
, ,
, ,
, ,
, ,
,
};
/**< 对p数组进行排序,先按x小的,如果x相等,再按y小的排序 */
int main()
{
sort(p, p + );
cout<<"p[]:";
for (int i = ; i < ; i++)
cout<<'('<<p[i].x<<','<<p[i].y<<')';
cout<<endl;
return ;
}
也可以自己用自定义的比较函数或比较对象来排序。
运行结果:
p[]:(,)(,)(,)(,)(,)
下面就来讲讲qsort函数
/**< 对于下面的a数组,如何使用qsort来排序呢,先看看代码实现 */
#include <iostream>
#include <algorithm> using namespace std; int a[] = {, , , , };
int cmp(const void *a, const void *b)/**< 从小到大排序 */
{
return ((*(int *)a) - (*(int *)b);
}
int main()
{
qsort(a, , sizeof(a[]), cmp);
cout<<"a[]:";
for (int i = ; i < ; i++)
cout<<' '<<a[i];
return ;
}
运行结果:
a[]:
对于整数和字符类型可以直接返回差值,
但对于实数需要注意,因为比较函数返回的是int类型。
例如:
#include <iostream>
#include <algorithm> using namespace std; double a[] = {2.4, 1.5, 3.4, 5.5, 1.6};
int cmp(const void *a, const void *b)/**< 从小到大排序 */
{
return (*(double *)a) > (*(double *)b) ? : -;
}
int main()
{
qsort(a, , sizeof(a[]), cmp);
cout<<"a[]:";
for (int i = ; i < ; i++)
cout<<' '<<a[i];
return ;
}
运行结果:
a[]: 1.5 1.6 2.4 3.4 5.5
还是先来看看qsort函数原型
_CRTIMP void __cdecl qsort(void*, size_t, size_t, int (*)(const void*, const void*));
对于为何要那样定义比较函数应该有个大概明白了吧。
原型要与int (*)(const void*, const void*)(函数指针的应用)对应。
qsort也可对自定义类型排序
#include <iostream>
#include <algorithm> using namespace std;
struct point
{
int x, y;
};
point p[] =
{
, ,
, ,
, ,
, ,
,
}; int cmp(const void *a, const void *b)
{
point *pa = (point *)a;
point *pb = (point *)b;
if (pa->x == pb->y)
return pa->y - pb->y;
return pa->x - pb->y;
}
/**< 对p数组进行排序,先按x小的,如果x相等,再按y小的排序 */
int main()
{
qsort(p, , sizeof(p[]), cmp);
cout<<"p[]:";
for (int i = ; i < ; i++)
cout<<'('<<p[i].x<<','<<p[i].y<<')';
cout<<endl;
return ;
}
运行结果:
p[]:(,)(,)(,)(,)(,)
对于其它自定义类型也是类似。
sort和qsort函数的平均时间复杂度都是NlogN,log以2为底。
都在algorithm文件里,同时别忘了使用命名空间
using namespace std。
看到这以后就不用在担心自己不会写排序函数了,学会了这两个函数,应付大部分排序就足够用了。
stl sort和qsort的使用的更多相关文章
- STL sort()函数
C++之所以得到这么多人的喜欢,是因为它既具有面向对象的概念,又保持了C语言高效的特点.STL 排序算法同样需要保持高效.因此,对于不同的需求,STL提供的不同的函数,不同的函数,实现的算法又不尽相同 ...
- c++ STL sort struct comp
详细解说 STL 排序(Sort) http://www.cppblog.com/mzty/archive/2005/12/15/1770.html 详细解说 STL 排序(Sort) 作者Winte ...
- STL sort 函数实现详解
作者:fengcc 原创作品 转载请注明出处 前几天阿里电话一面,被问到STL中sort函数的实现.以前没有仔细探究过,听人说是快速排序,于是回答说用快速排序实现的,但听电话另一端面试官的声音,感觉不 ...
- sort 与 qsort
很长一段时间搞不明白 sort 和 qsort 的区别,平时在写程序时习惯了使用 sort ,因为它用起来比 qsort 要简单的多 , 这里详细介绍一下 sort 与 qsort : 给出一个数组 ...
- STL::sort函数实现
声明:本文参考链接:STL::sort实现. 排序是面试中经常被问及的算法基础知识点,虽然实际应用中不会直接使用,但是理解这些简单的算法知识对于更复杂更实用的算法有一定的帮助,毕竟面试总不能问的太过深 ...
- STL sort 函数实现详解 ZZ
前几天阿里电话一面,被问到STL中sort函数的实现.以前没有仔细探究过,听人说是快速排序,于是回答说用快速排序实现的,但听电话另一端面试官的声音,感觉不对劲,知道自己回答错了.这几天特意看了一下,在 ...
- STL sort
STL的sort()算法,数据量大时采用Quick Sort,分段递归排序,一旦分段后的数据量小于某个门槛,为避免Quick Sort的递归调用带来过大的额外负荷,就改用Insertion Sort. ...
- c++ stl sort example
c++ stl sort函数使用举例: #include <iostream> #include<vector> #include<algorithm> #incl ...
- C++中sort()及qsort() (不完整介绍)
在平时刷算法题和oj的时候,排序算法是最经常用到的算法之一:且在各类算法书的目录中 也通常是将各种排序算法放在最前面来讲,可见排序算法的重要性.可能许多人都在算法书中有学过冒泡.快速排序的方法,也都大 ...
随机推荐
- bzoj 2756 [SCOI2012]奇怪的游戏【二分+最大流】
达成成就:为二分调参 !:多次memset的话要把数组大小开严格一点,否则会T 看到网格图,首先黑白染色. 注意到每次操作都是在一个黑格子和一个白格子上进行的,也就是说,最后黑格子数字和白格子数字和的 ...
- bzoj 4070: [Apio2015]雅加达的摩天楼【spfa】
明明是个最短路却有网络流一样的神建图= A = 首先要是暴力建图的话最坏有O(nm)条边.所以优化建图. 考虑分块思想,设bs=sqrt(n),对于p大于bs的,直接连边即可,最多有sqrt(n)条, ...
- [Swift通天遁地]一、超级工具-(3)带切换图标的密码文本框
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- Canvas 入门案例
五. Canvas 入门案例 1. canvas 圆形绘制 <!DOCTYPE html> <html lang="en"> <head> ...
- 《开源自主OdooERP部署架构指南》试读:第二章数据库服务构建
文/开源智造联合创始人老杨 本文来自<开源自主OdooERP部署架构指南>的试读章节.书籍尚未出版,请勿转载.欢迎您反馈阅读意见. 使用apt.postgresql.org 您可以选择使用 ...
- 1.1.1最短路(Floyd、Dijstra、BellmanFord)
转载自hr_whisper大佬的博客 [ 一.Dijkstra 比较详细的迪杰斯特拉算法讲解传送门 Dijkstra单源最短路算法,即计算从起点出发到每个点的最短路.所以Dijkstra常常作为其他算 ...
- RHEL5.6环境下Oracle10g单主机安装步骤记录
操作环境:RedHat Enterprise Linux 5.6 安装版本:Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 64 ...
- leetcode 484. Find Permutation 思维题
https://leetcode.com/contest/leetcode-weekly-contest-16a/problems/find-permutation/ 设原本的数字是0,那么按照它的D ...
- Snort里的规则目录文件解读(图文详解)
不多说,直接上干货! snort的规则啊,是基于文本的,它通常存在于snort程序目录中或者子目录中,规则文件按照不同的组,进行分类存放的. snort的安装目录 [root@datatest sno ...
- TC 609DIV2(950)
Problem Statement Vocaloids Gumi, Ia, and Mayu love singing. They decided to make an album comp ...