当前位置:网站首页>55.【sort函数的升序降序】
55.【sort函数的升序降序】
2022-08-02 17:36:00 【李在奋斗……】
1.如果用sort进行排序,那么需要用头文件#include<algorithm>
2、sort模板含三个参数:
sort (begin,end,cmp)
参数begin:要排序数组的起始地址(第一个数据的地址)
参数end:最后一个数据的下一个数据的地址
参数cmp:若这个参数不写,默认为升序
【默认不写为升序】
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
int main()
{
int i;
string s;
cin >> s;
sort(s.begin(), s.end());
for (i = 0; i < s.length(); i++)
{
cout << s[i] << " ";
}
return 0;
}

【写了cmp,但a<b,依然为升序】
#include <iostream>
#include <algorithm>
using namespace std;
bool cmp(int a,int b)
{
return a<b; //或则在cmp函数里面写return a<b,也是升序
}
int main()
{
int a[]={6,5,8,4,3,2},i;
sort(a,a+6,cmp);
for(i=0;i<5;i++)
{
cout<<a[i]<<" ";
}
return 0;
}
========================================
【写了cmp,且a>b,降序】
#include <iostream>
#include <algorithm>
using namespace std;
bool cmp(int a,int b)
{
return a>b; //注意这里是a>b是指降序
}
int main()
{
int a[]={6,5,8,4,3,2},i;
sort(a,a+6,cmp);
for(i=0;i<5;i++)
{
cout<<a[i]<<" ";
}
return 0;
}
边栏推荐
- docker安装Oracle之后常用的一些命令
- 「全球数字经济大会」登陆 N 世界,融云提供通信云服务支持
- Wechat Gymnasium Appointment Mini Program Graduation Design Finished Work (5) Task Book
- What is the difference between erp system and wms system
- Arduino hardware programming introduction to language learning
- 蔚来杯2022牛客暑期多校训练营5 ABCDFGHK
- 小程序毕设作品之微信体育馆预约小程序毕业设计成品(5)任务书
- golang学习之七:并发编程基础(goroutine、channel、select)
- Smart Microelectronics Releases Low-Power MM32L0130 Series MCU Products
- golang源码分析(8):m、p、g、shedt、sudog
猜你喜欢

mysql四种隔离级别

Taking advantage of cloud-network integration, e-Surfing Cloud has paved the way for digital transformation for government and enterprises

Flink Learning 9: Configure the idea to develop the flink-Scala program environment

土巴兔IPO五次折戟,互联网家装未解“中介”之痛

vulnhub W34kn3ss: 1

Security First: Tools You Need to Know to Implement DevSecOps Best Practices

LeetCode·每日一题·

新特性解读 | MySQL 8.0 GIPK 不可见主键

解决多版本jar包冲突问题

罗敏背后是抖音
随机推荐
究极异常处理逻辑——多层次异常的处理顺序
Several common cross-domain solutions
golang源码阅读(11)GO中各个目录的功能
玩转云端 | 天翼云对象存储ZOS高可用的关键技术揭秘
Mysql和Redis如何保证数据一致性
二舅“反转”了?
判断文件属主
9月起中国给予多哥等16国98%税目产品零关税待遇
全面认识二极管,一篇文章就够了
Smart Contract Security - delegatecall (1)
IDEA相关配置(特别完整)看完此篇就将所有的IDEA的相关配置都配置好了、设置鼠标滚轮修改字体大小、设置鼠标悬浮提示、设置主题、设置窗体及菜单的字体及字体大小、设置编辑区主题、通过插件更换主题
动力电池扩产潮,宁德时代遭围剿
The days of patching are more difficult than the days of writing code
vulnhub W34kn3ss: 1
NeRF: The Secret of 3D Reconstruction Technology in the Popular Scientific Research Circle
KunlunBase 1.0 is released!
使用lodash替换js字符串中的变量
LeetCode·每日一题·
golang源码分析(8):m、p、g、shedt、sudog
golang源码分析(4):select
