#include <algorithm>中sort的一般用法
1、sort函数的时间复杂度为n*log2(n),执行效率较高。
2、sort函数的形式为sort(first,end,method)//其中第三个参数可选。
3、若为两个参数,则sort的排序默认是从小到大,见如下例子
- #include<iostream>
- #include<algorithm>
- using namespace std;
- int main()
- {
- int a[10]={9,6,3,8,5,2,7,4,1,0};
- for(int i=0;i<10;i++)
- cout<<a[i]<<endl;
- sort(a,a+10); //可以看出,两个参数为均地址,a为起始,a+10为结束位置
- for(int i=0;i<10;i++)
- cout<<a[i]<<endl;
- return 0;
- }
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int a[10]={9,6,3,8,5,2,7,4,1,0};
for(int i=0;i<10;i++)
cout<<a[i]<<endl;
sort(a,a+10); //可以看出,两个参数为均地址,a为起始,a+10为结束位置
for(int i=0;i<10;i++)
cout<<a[i]<<endl;
return 0;
}
4、若为三个参数,则需要写一个cmp函数(此名称cmp可变),用于判断是从小到大排序还是从大到小排序。
(1)需要排序的数组直接为int类型,则见如下例子(从大到小排序)
- #include <algorithm>
- #include <iostream>
- using namespace std;
- bool com(int a,int b)
- {
- return a>b;
- }
- int main()
- {
- int a[10]={9,6,3,8,5,2,7,4,1,0};
- for(int i=0;i<10;i++)
- cout<<a[i]<<endl;
- sort(a,a+10,com);//在这里就不需要对com函数传入参数
- for(int i=0;i<10;i++)
- cout<<a[i]<<endl;
- return 0;
- }
#include <algorithm>
#include <iostream>
using namespace std; bool com(int a,int b) { return a>b; } int main() { int a[10]={9,6,3,8,5,2,7,4,1,0}; for(int i=0;i<10;i++) cout<<a[i]<<endl; sort(a,a+10,com);//在这里就不需要对com函数传入参数 for(int i=0;i<10;i++) cout<<a[i]<<endl; return 0; }
(2)如果想依照一个结构体内的一个int型的属性参数进行排序,则见如下例子(从大到小排列)
- #include <iostream>
- #include <algorithm>
- using namespace std;
- struct node {
- int a;
- //.........
- //
- };
- bool cmp(node x,node y)
- {
- if(x.a != y.a)
- return (x.a > y.a);
- }
- void main(void)
- {
- int i;
- node N_t[5];
- for(i=0; i<5; i++)
- {
- cin>>N_t[i].a;
- }
- sort(N_t, N_t+5, cmp);
- for(i=0; i<5; i++)
- {
- cout<<N_t[i].a;
- }
- }
#include <algorithm>中sort的一般用法的更多相关文章
- <algorithm>中sort()函数的用法
先说一下,本篇文章我没有讲sort()实现排序的原理,我写在另一篇文章中了,如果想了解的话,可以看一下,附上链接:https://www.cnblogs.com/buanxu/p/12772700.h ...
- C++<algorithm>中sort的比较函数写法(转)
转自:http://www.wl566.com/biancheng/98907.html C++<algorithm>中sort的比较函数写法,有需要的朋友可以参考下. 定义排序函数: 方 ...
- JS中sort()方法的用法,参数以及排序原理
sort() 方法用于对数组的元素进行排序,并返回数组.默认排序顺序是根据字符串Unicode码点.语法:arrayObject.sort(sortby):参数sortby可选.规定排序顺序.必须是函 ...
- python中sort和sorted用法的区别
Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列 一,最简单的排序 1.使用sort排序 my_list = [3 ...
- c++中sort基础用法
用法一:数组排序 对一个数组进行升序排序 #include <algorithm> #include <iostream> #include <cstdio> us ...
- STL之sort函数的用法
说明:本文仅供学习交流,转载请标明出处,欢迎转载! STL封装了一个排序算法,该算法相应的头文件为#include<algorithm>,我们能够依据须要对一个数组进行排序或者降序. so ...
- 转载 从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法
转载自:http://www.cnblogs.com/cj695/p/3863142.html sort函数在使用中非常好用,也非常简单,而且效率与冒泡或者选择排序不是一个数量级.本文就sort函数在 ...
- 【转】 从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法
sort函数在使用中非常好用,也非常简单,而且效率与冒泡或者选择排序不是一个数量级.本文就sort函数在vector中的用法分为sort函数入门用法与自定义comp比较函数比较结构体这两个最基本的功能 ...
- C++ 中的sort()排序函数用法
sort(first_pointer,first_pointer+n,cmp) 该函数可以给数组,或者链表list.向量排序. 实现原理:sort并不是简单的快速排序,它对普通的快速排序进行了优化,此 ...
随机推荐
- java beanUtils框架
beanUtils是Apache觉得sun公司的内省不够爽,自己又开发了一套可以操作JavaBean的API 所以beanUtils是第三方jar包,使用beanUtils要导包: 在工程目录下新建一 ...
- 使用Elasticsearch 与 NEST 库 构建 .NET 企业级搜索
使用Elasticsearch 与 NEST 库 构建 .NET 企业级搜索 2015-03-26 dotNET跨平台 最近几年出现的云计算为组织和用户带来了福音.组织对客户的了解达到前所未有的透彻, ...
- Android WiFi开发
概述 介绍Android WiFi的扫描.连接.信息.以及WiFi热点等等的实现,并用代码实现. 详细 代码下载:http://www.demodashi.com/demo/10660.html 一. ...
- [转]网易云音乐Android版使用的开源组件
原文链接 网易云音乐Android版从第一版使用到现在,全新的 Material Design 界面,更加清新.简洁.同样也是音乐播放器开发者,我们确实需要思考,相同的功能,会如何选择.感谢开源,让我 ...
- windows添加开机启动项
http://www.cnblogs.com/jokey/archive/2010/06/17/1759370.html添加开机启动项(通过注册表) 例子:增加QQ开机启动项 第一步:找到注册表的启动 ...
- SVN解决创建文件时不带锁
解决创建文件时不带锁 C:\Documents and Settings\你的用户名\Application Data\Subversion 找到上面的用户路径 打开config添加 ### ...
- 用一个URL加一个JAVA壳做成一个安卓应用
若想获得更新版本欢迎加入我们的 “本地应用开发webapp"群,一起协力开发一处编写处处运行的“本地应用”.我们将一直开源,将智慧共享,只有这样我们才能一起将应用体验做好! 特点: 1.程序 ...
- 在 Asp.NET MVC 中使用 SignalR 实现推送功能 [转]
在 Asp.NET MVC 中使用 SignalR 实现推送功能 罗朝辉 ( http://blog.csdn.net/kesalin ) CC许可,转载请注明出处 一,简介 Signal 是微软支持 ...
- PLSQL_数据结构类型的解析(概念)
2014-06-02 Created By BaoXinjian
- AOV网与拓扑排序
在一个表示工程的有向图中,用顶点表示活动,用弧表示活动之间的优先关系,这样的有向图为顶点表示活动的网,我们称之为AOV网(Activity on Vextex Network).AOV网中的弧表示活动 ...