目录

  • 一.简单升序排序
    • (1)准备
    • (2)函数格式
    • (3)完整代码
    • (4)结果
  • 二.简单降序排序
    • (1)函数格式
    • (2)完整代码
    • (3)结果
  • 三.自定义函数排序
    • (1)函数格式
    • (2)代码
    • (3)结果

最近刷力扣的时候遇到了好多vector数组自定义排序问题,这里先来粗略的总结一下。

vector是C++的STL中一个重要的容器嘛,STL不用说也知道它的功能十分强大,所以有一些内置排序函数。下面来细说一下。


一.简单升序排序

(1)准备

vector头文件,用来引入vector数组

algorithm头文件,用来引入sort函数

(2)函数格式

sort(name.begin(),name.end());

name.begin()表示指向数组头的迭代器,name.end()表示指向数组尾下一个位置的迭代器,该式表示将叫name的vector元素按从小到大进行升序排序。

(3)完整代码

#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<vector>
using namespace std;
int main(){int ans[6]={5,7,3,8,1,2};vector<int>num;cout<<"before sorted: ";for(int i=0;i<6;i++){cout<<ans[i]<<" ";num.emplace_back(ans[i]);}cout<<endl;cout<<"after sorted: ";//按升序排序sort(num.begin(),num.end());for(int i=0;i<num.size();i++){cout<<num[i]<<" ";}system("pause");return 0;
}

(4)结果


二.简单降序排序

(1)函数格式

sort(name.rbegin(),name.rend());

name.rbegin()表示指向数组尾的迭代器,name.rend()表示指向数组头前一个位置的迭代器,该式表示将叫name的vector元素按从大到小进行降序排序。

(2)完整代码

#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<vector>
using namespace std;
int main(){int ans[6]={5,7,3,8,1,2};vector<int>num;cout<<"before sorted: ";for(int i=0;i<6;i++){cout<<ans[i]<<" ";num.emplace_back(ans[i]);}cout<<endl;cout<<"after sorted: ";//按降序排序sort(num.rbegin(),num.rend());for(int i=0;i<num.size();i++){cout<<num[i]<<" ";}system("pause");return 0;
}

(3)结果


三.自定义函数排序

我们在很多时候都要按照题意来进行排序,而且一般这个对象不止含有一个属性,所以我们需要来自定义它的排序方式。下面以一群学生为例,我们对他们身高进行降序排序,当身高相同时按名字中的字母顺序排序。

学生列表

name height
amy 165
hone 177
mike 175
jerry 169
jone 169
alex 165
amye 165

(1)函数格式

sort(res.begin(),res.end(),[&](const typename&a,const typename&b)->bool{
    return (输入你的排序标准);
});

(2)代码

#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<string>
#include<unordered_map>
using namespace std;
int main(){//我们将学生信息存入map中unordered_map<string,int>map;string name;int height;for(int i=0;i<7;i++){cin>>name>>height;map[name]=height;}//将学生名字存入vector数组中vector<string>res;for(auto &[key,value]:map){res.emplace_back(key);}//自定义排序,按身高降序,身高相同时则按名字升序排列sort(res.begin(),res.end(),[&](const string&a,const string&b)->bool{return map[a]==map[b]?a<b:map[a]>map[b];});//输出排列后的顺序cout<<endl;cout<<"after sorted: "<<endl;for(int i=0;i<res.size();i++){cout<<res[i]<<"   "<<map[res[i]]<<endl;}system("pause");return 0;
}

(3)结果

【C++】vector数组排序相关推荐

  1. C++对高维vector数组排序 sort()函数第三个参数自定义

    我们都用过sort()函数,对于一个vector vec;我们可以很轻松的写出: //sort()函数默认使用升序排列 sort(vec.begin(),vec.end()); 但是当我们碰到vect ...

  2. sort()、stable_sort()、partial_sort()、nth_element()、greater()、is_sorted()

    sort(a, a+5); // 默认从小到大,int数组的排序 sort(v.begin(), v.end()); // vector数组排序 sort中使用的是快排和插排 stable_sort( ...

  3. C++ STL unordered_map按照value排序

    参考:unorderedmap根据键排序_charon的博客-CSDN博客 pair常见用法详解_ZMST的博客-CSDN博客 自定义排序函数,把map加入vector数组,对vector数组排序 头 ...

  4. LeetCode 16最接近的三数之和

    力扣 思路 排序+双指针 枚举第一个数a,对剩下的两个元素b,c,希望它们的和最接近target-a 1.如果它们在原数组中枚举的范围没有任何规律可言,只能用两重循环来枚举所有情况 ->考虑对数 ...

  5. vector排序|vector多维数组排序|vector自定义排序|不改变相同元素相对顺序比较

    vector<int>排序 头文件:#include <algorithm> 示例如下,默认升序 #include <iostream> #include < ...

  6. [CareerCup] 11.2 Sort Anagrams Array 异位词数组排序

    11.2 Write a method to sort an array of strings so that all the anagrams are next to each other. 这道题 ...

  7. C++ 对二维数组排序 升序 降序

    点击查看更多通信与专业知识 今天在做下面这道题的时候糊涂了,把二维数组的排序做错了. 题目 假设有打乱顺序的一群人站成一个队列,数组 people 表示队列中一些人的属性(不一定按顺序).每个 peo ...

  8. STL之vector,数组线性容器array,list容器,算法find,find_if,bind1st,仿函数

     1.STL(Standard Template Library,是用泛型技术来设计完成的实例)的概念与组成 Iterator(迭代器) Container(容器) Algorithm(算法) A ...

  9. 数组排序最小复杂度_进行排序的最小缺失数

    数组排序最小复杂度 Problem statement: 问题陈述: Given an array of n integers. Find the minimum number of elements ...

最新文章

  1. Linux的编译器vi之最详细介绍
  2. python最简单的架构_Python实现简单状态框架的方法
  3. 「mysql优化专题」高可用性、负载均衡的mysql集群解决方案(12)
  4. [云炬创业基础笔记]第六章商业模式测试9
  5. dubbo启动时检查服务
  6. DOM-3 【utils/待讲评】节点属性、方法、封装方法、DOM结构
  7. Blog.Core高级进阶:共赴五年之约
  8. linux chgrp
  9. 华为nova2s云相册在哪里_华为反人类的用户体验
  10. thinkphp LoginAction.class.php 登录模块
  11. (转)Python之区块链入门
  12. element-UI级联选择器的使用Cascader
  13. vue2.0 axios封装
  14. 美团到店Java二面:TCP 糊涂窗口综合症面试题汇总解析
  15. 5个理由告诉你为什么用NAS网络存储
  16. MTSP遗传算法解决
  17. Kingston U盘 量产
  18. 傅里叶变换F(f)与F(w)的探究——以余弦函数为例
  19. 思维导图:从Xmind到docsify博客
  20. 一起业务逻辑导致的ogg故障

热门文章

  1. android 图片编辑工具,图片编辑工具下载
  2. Docker学习:跨宿主机通信 | overlay和macvlay(理论+实战篇)
  3. 29 深度玻尔兹曼机 Deep Boltzmann Machine
  4. Map集合(HashMap,TreeMap)学习总结以及经典案例
  5. mongoDB的安装与配置和客户端的使用
  6. 旋转矩阵求解欧拉角Python实现
  7. 突破16%“生死线”!央视点名鸿蒙,华为再次“改口”
  8. 确定了?《英雄联盟》手游12月国服公测,主播已参与内测
  9. android智能客户,南宁Android智能机器人批发客户至上
  10. 小区宽带计费解决方案