sort类函数:

函数名 功能描述
sort 对给定区间所有元素进行排序
stable_sort 对给定区间所有元素进行稳定排序
partial_sort 对给定区间所有元素部分排序
partial_sort_copy 对给定区间复制并排序
nth_element 找出给定区间的某个位置对应的元素
is_sorted 判断一个区间是否已经排好序
partition 使得符合某个条件的元素放在前面
stable_partition 相对稳定的使得符合某个条件的元素放在前面

需要头文件<algorithm>

语法描述:sort(begin,end,cmp),cmp参数可以没有,如果没有默认非降序排序。

以int为例的基本数据类型的sort使用

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int main()
{
int a[]={,,,,};
sort(a,a+);
for(int i=;i<;i++)
cout<<a[i]<<' ';
return ;
}

因为没有cmp参数,默认为非降序排序,结果为:

1 2 3 4 5

若设计为非升序排序,则cmp函数的编写:

bool cmp(int a,int b)

{

  return a>b;

}

其实对于这么简单的任务(类型支持“<”、“>”等比较运算符),完全没必要自己写一个类出来。标准库里已经有现成的了,就在functional里,include进来就行了。functional提供了一堆基于模板的比较函数对象。它们是(看名字就知道意思了):equal_to<Type>、not_equal_to<Type>、greater<Type>、greater_equal<Type>、less<Type>、less_equal<Type>。对于这个问题来说,greater和less就足够了,直接拿过来用:

  • 升序:sort(begin,end,less<data-type>());
  • 降序:sort(begin,end,greater<data-type>()).
  • int  main ( )
    {
    int a[]={,,,,,,,,,},i;
    for(i=;i<;i++)
    cout<<a[i]<<endl;
    sort(a,a+,greater<int>());
    for(i=;i<;i++)
    cout<<a[i]<<endl;
    return ;
    }

    引用数据类型string的使用

  • 一个字符串间的个字符排序:
  • 使用迭代器可以完成顺序排序
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int main()
{
string str("hello world");
sort(str.begin(),str.end());
cout<<str;
return ;
}

结果:空格dehllloorw

使用反向迭代器可以完成逆序排序

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int main()
{
string str("hello world");
sort(str.rbegin(),str.rend());
cout<<str;
return ;
}

结果:wroolllhde空格

字符串间的比较排序

#include<iostream>
#include<cstring >
#include<algorithm>
using namespace std;
int main()
{
string a[];
for(int i=;i<;i++)
getline(cin,a[i]);
sort(a,a+);
for(int i=;i<;i++)
cout<<a[i]<<endl;
return ;
}

以结构体为例的二级排序

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
struct link
{
int a,b;
};
bool cmp(link x,link y)
{
if(x.a==y.a)
return x.b>y.b;
return x.a>y.a;
}
int main()
{
link x[];
for(int i=;i<;i++)
cin>>x[i].a>>x[i].b;
sort(x,x+,cmp);
for(int i=;i<;i++)
cout<<x[i].a<<' '<<x[i].b<<endl;
return ;
}

c++sort函数的使用总结的更多相关文章

  1. STL sort 函数实现详解

    作者:fengcc 原创作品 转载请注明出处 前几天阿里电话一面,被问到STL中sort函数的实现.以前没有仔细探究过,听人说是快速排序,于是回答说用快速排序实现的,但听电话另一端面试官的声音,感觉不 ...

  2. 神奇的sort()函数

    今天来谈一谈sort()函数,sort() 方法用于对数组的元素进行排序,用法为arrayObject.sort(sortby):括号中的为可选参数,准确来说应该是一个函数,这个函数用来规定排序方法, ...

  3. qsort函数、sort函数【转】

    http://blog.163.com/yuhua_kui/blog/static/9679964420142195442766/ 先说明一下:qsort和sort,只能对连续内存的数据进行排序,像链 ...

  4. C中的qsort函数和C++中的sort函数的理解与使用

    一.qsort()函数 原型:_CRTIMP void __cdecl qsort (void*, size_t, size_t,int (*)(const void*, const void*)); ...

  5. python 中的sort 和java中的Collections.sort()函数的使用

    x=[1,2,3] x.sort()对的,x这个都变了 y=x.sort()错误 y=sorted(x)对的,x拍好序的一个副本 python中用匿名函数和自定义函数排序:(很奇怪的是比较函数返回的是 ...

  6. sort函数用法

    原文链接:http://blog.csdn.net/csust_acm/article/details/7326418 sort函数的用法 做ACM题的时候,排序是一种经常要用到的操作.如果每次都自己 ...

  7. Perl Sort函数用法总结和使用实例

    一) sort函数用法 sort LISTsort BLOCK LISTsort SUBNAME LIST sort的用法有如上3种形式.它对LIST进行排序,并返回排序后的列表.假如忽略了SUBNA ...

  8. C++ sort函数

    (一)为什么要用c++标准库里的排序函数 Sort()函数是c++一种排序方法之一,学会了这种方法也打消我学习c++以来使用的冒泡排序和选择排序所带来的执行效率不高的问题!因为它使用的排序方法是类似于 ...

  9. 使用STL库sort函数对vector进行排序

    使用STL库sort函数对vector进行排序,vector的内容为对象的指针,而不是对象. 代码如下 #include <stdio.h> #include <vector> ...

  10. C++ algorithm 里的sort函数应用

    MSDN中的定义: template<class RanIt>    void sort(RanIt first, RanIt last); //--> 1)template< ...

随机推荐

  1. [基础]Android 应用的启动

    Android 应用的启动模式分为两种,一种是通过启动器(Launcher)启动,另一种是通过Intent消息启动. 如果在通过Intent 消息启动前,希望判断欲启动的应用是否已经安装, 目前有两种 ...

  2. centos7虚拟机下python3安装matplotlib遇到的一些问题

    1.安装位置 centos7虚拟机+python3.6 2.问题 2.1如果是使用的python2版本可以使用如下方式, #yum search matplotlib 返回如下: 已加载插件:fast ...

  3. django-url的分发

    1)url的分发: 1,首先在全局的url里面的路径中写好,你要分发的路径名. 2,并且在你要分发的路径下,创好新的url文件. 在分发的路径名里面,把全局url里面的代码,复制过来 3,最后在浏览器 ...

  4. 锁(1):spin_lock & mutex_lock的区别? .

    为什么需要内核锁? 多核处理器下,会存在多个进程处于内核态的情况,而在内核态下,进程是可以访问所有内核数据的,因此要对共享数据进行保护,即互斥处理   有哪些内核锁机制? (1)原子操作 atomic ...

  5. git添加删除远程tag

    git tag -a test20190108_1 -m "fix bug" git push origin test20190108_1 git push origin :ref ...

  6. CMDB资产管理系统开发【day25】:表结构设计2

    表结构设计1详细注释代码 # _*_coding:utf-8_*_ __author__ = 'luoahong' from assets.myauth import UserProfile from ...

  7. Mybatis-批量执行

    一.使用动态SQL 中的 Foreach 批量插入 1.MySQL // 实体类 public class MyUser { private Integer id; private String na ...

  8. SpringBoot系列: Pebble模板引擎

    ===============================Java 模板引擎选择===============================SpringBoot Starter项目向导中可选的J ...

  9. [物理学与PDEs]第1章第3节 真空中的 Maxwell 方程组, Lorentz 力 3.2 Lorentz 力

    1. Lorentz 假定, 不论带电体的运动状态如何, 其所受的力密度 (单位体积所受的力) 为 $$\bex {\bf F}=\rho {\bf E}+{\bf j}\times{\bf B} = ...

  10. 在Spring框架中bean配置文件中constructor-arg标签中没有name元素?

    bean配置文件出现错误的依赖: <beans <beans xmlns="http://www.springframework.org/schema/beans"   ...