当前位置: 首页 > news >正文

按字符串长度升序,长度相同则按字典序

优先按字符串长度升序排序

如果长度相同,则按字典序升序排序

使用 set<string, cmp>

#include <bits/stdc++.h>
using namespace std;// 比较器
struct cmp{bool operator()(const string& a ,const string& b) const{if(a.size() == b.size())return a<b;return a.size()  < b.size();}
};//记得 ;int main() {set<string,cmp> myset;myset.insert("apple");myset.insert("kiwi");myset.insert("pear");myset.insert("grape");myset.insert("banana");myset.insert("mango");for( auto & s : myset){cout << s << endl;}return 0;
}

输出:

kiwi
pear
apple
grape
mango
banana
 

#include <bits/stdc++.h>
using namespace std;int main() {priority_queue<string,vector<string>> pq;pq.push("apple");pq.push("kiwi");pq.push("pear");pq.push("grape");pq.push("banana");pq.push("mango");while(!pq.empty()){string str = pq.top();pq.pop();cout << str << endl;}return 0;
}

默认大根堆

内部按字典序建最大堆,pq.top() 永远是字典序最大的字符串

输出:

pear
mango
kiwi
grape
banana
apple

按字符串长度排序的大根堆

字符串长度越长,优先级越高pq.top() 会返回当前堆中最长的字符串

当长度相同,它们会被当作“相等”处理,顺序不确定(非稳定)。

#include <bits/stdc++.h>
using namespace std;// 比较器
// 注意:priority_queue 比较器是 “谁优先级低” 返回 true
struct cmp{bool operator()(const string& a ,const string& b) const{// if(a.size() == b.size())// return a<b;return a.size()  < b.size(); //长的字符串优先(更长 = 优先级更高)}
};
// 举例:若 a = "kiwi", b = "banana",则 a.size() < b.size() 返回 true,表示 kiwi 的优先级低int main() {// 定义一个优先队列,存放 string 类型// 使用 vector<string> 作为底层容器,cmp 为比较器priority_queue<string,vector<string> ,cmp > pq;pq.push("apple");pq.push("kiwi");pq.push("pear");pq.push("grape");pq.push("banana");pq.push("mango");while(!pq.empty()){string str = pq.top();pq.pop();cout << str << " ";}return 0;
}

最短的字符串先出来,长度相同则字典序小的先出来

#include <bits/stdc++.h>
using namespace std;// 比较器
struct cmp{bool operator()(const string& a ,const string& b) const{if(a.size() == b.size())return a > b; //如果两个字符串长度相等,按字典序升序排列(小的优先)return a.size()  > b.size(); //按长度升序排列(短的优先)}
};int main() {priority_queue<string,vector<string> ,cmp > pq;pq.push("apple");pq.push("kiwi");pq.push("pear");pq.push("grape");pq.push("banana");pq.push("mango");while(!pq.empty()){string str = pq.top();pq.pop();cout << str << " ";}return 0;
}

输出:kiwi pear apple grape mango banana

相关文章:

  • 【Linux系统】Linux基础指令(详解Linux命令行常用指令,每一个指令都有示例演示)
  • 30天开发操作系统 第26天 -- 为窗口移动提速
  • 实现AWS Data Pipeline安全地请求企业内部API返回数据
  • 2026《数据结构》考研复习笔记四(第一章)
  • 蓝桥杯 二进制问题 刷题笔记
  • Linux操作系统简介:从开源内核到技术生态
  • BeautifulSoup 库的使用——python爬虫
  • AWS EC2完全指南:如何快速搭建高性能云服务器?
  • maven的安装与配置、IDEA集成maven
  • BEVDet: High-Performance Multi-Camera 3D Object Detection in Bird-Eye-View
  • 实操基于MCP驱动的 Agentic RAG:智能调度向量召回或者网络检索
  • 23、.NET和C#有什么区别?
  • 鸿蒙ArkUI之布局实战,线性布局(Column,Row)、弹性布局(Flex)、层叠布局(Stack),详细用法
  • C语言 —— 铭纹织构未诞之镜 - 预处理详解
  • AIGC通信架构深度优化指南
  • 【Qt】QMainWindow类
  • leetcode 1035. Uncrossed Lines
  • css3新特性第三章(文本属性)
  • AI Agent破局:智能化与生态系统标准化的颠覆性融合!
  • 【技术派后端篇】Redis实现统计计数
  • 中国在建结构第一高楼“天津117大厦”将复工,预计2027年完工
  • 海南医科大学继续开展部门正职竞聘上岗,致力营造“谁有本事谁来”
  • “明制美学”的舞台呈现,陆川导演首部舞剧《天工开物》
  • 上海崇明“人鸟争食”何解?检察机关推动各方寻找最优解
  • 守护体面的保洁员,何时能获得体面?|离题
  • 西北政法大学推无手机课堂,有学生称要求全交,学校:并非强制