C++STL之priority_queue的简单使用_在c++stl中priority_queue用法

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新网络安全全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上网络安全知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加V获取:vip204888 (备注网络安全)
img

正文

void menu()
{
cout << “1.入队 2.出队” << endl;
cout << “3.生成 4.队头” << endl;
cout << “********5.交换 6.排序规则展示” << endl;
cout << “7.自定义 8.退出” << endl;
}
//入队 无参数
void Push_pq()
{
int n;
cout << “请输入一个整数:” << endl;
cin >> n;
pq.push(n);
cout << “已入队,队列大小:”<<pq.size()<< endl;
}
//出队 无参数
void Pop_pq()
{
int n;
if (!pq.empty())
{
n = pq.top();
pq.pop();
cout << “队头:” << n << “已出队” << endl;
if(pq.empty())cout << “队列已空!!!” << endl;
}
else
{
cout << “队列已空!!!” << endl;
}
}
//插入 直接在某个位置生成,效率更高
void Insert()
{
int n;
cout << “请输入一个整数:” << endl;
cin >> n;
pq.emplace(n);
cout << “已生成,队列大小:” << pq.size() << endl;
}
//队头 获取队头,可修改
void Top()
{
int n;
if (!pq.empty())
{
cout << “队头为:” << pq.top() << endl;
}
cout << “是否修改? 0.不修改 1.修改” << endl;
cin >> n;
if (n) {
cout << “请输入一个整数:” << endl;
cin >> n;
int value = pq.top();
value = n;
}
}
//交换两个优先队列 用的比较少
void Swap()
{
int values[4] = {1,2,3,4};
priority_queue pq_cp(values,values+4);
pq.swap(pq_cp);
cout << “交换后为{1,2,3,4}” << endl;
}
//排序规则展示
void Priority()
{
int int_values[5] = { 1,3,2,0,4 };
priority_queue int_pq(int_values, int_values + 5);
cout << “{ 1,3,2,0,4 }整型默认为从大到小(std::less):” << endl;
while (!int_pq.empty())
{
cout << int_pq.top() << " “;
int_pq.pop();
}
cout<< endl;
priority_queue<int,vector,greater> int_pq_new(int_values, int_values + 5);
cout << “{ 1,3,2,0,4 }排序规则改为从小到大(std::greater):” << endl;
while (!int_pq_new.empty())
{
cout << int_pq_new.top()<<” ";
int_pq_new.pop();
}
cout << endl;

char char_values[5] = { 'b','a','1','c','?'};
priority_queue<char> char_pq(char_values, char_values + 5);
cout << "{ 'b','a','1','c','?'}字符型默认按ASCII为从大到小(std::less):" << endl;
while (!char_pq.empty())
{
	cout << char_pq.top() << " ";
	char_pq.pop();
}
cout << endl;
priority_queue<char, vector<char>, greater<char>> char_pq_new(char_values, char_values + 5);
cout << "{ 'b','a','1','c','?'}排序规则改为从小到大(std::greater):" << endl;
while (!char_pq_new.empty())
{
	cout << char_pq_new.top() << " ";
	char_pq_new.pop();
}
cout << endl;

string str_values[5] = { "bde","abc","bbe","cdf","cde" };
priority_queue<string> str_pq(str_values, str_values + 5);
cout << "{ 'b','a','1','c','?'}字符串型默认从左到右一个个字符按ASCII比较,大的优先级高(std::less):" << endl;
while (!str_pq.empty())
{
	cout << str_pq.top() << " ";
	str_pq.pop();
}
cout << endl;
priority_queue<string, vector<string>, greater<string>> str_pq_new(str_values, str_values + 5);
cout << "{ 'b','a','1','c','?'}排序规则改为从小到大(std::greater):" << endl;
while (!str_pq_new.empty())
{
	cout << str_pq_new.top() << " ";
	str_pq_new.pop();
}
cout << endl;

}
//自定义排序规则 偶数优先,同为偶数或奇数,大数优先
struct oddFirst
{
bool operator()(const int &a, const int &b)
{
if (a % 2 == 0 && b % 2 == 0)
{
return a < b ? true : false;
}
else if (a % 2 != 0 && b % 2 != 0)
{
return a < b ? true : false;
}
else
{
return a % 2 == 0 ? false : true;
}
}
};
//自定义排序规则
void Priority_self()
{
int int_values[5] = { 1,3,2,0,4 };
priority_queue<int, std::vector, oddFirst> mypq(int_values,int_values+5);
cout << “{ 1,3,2,0,4 }整型自定义偶数优先排序:” << endl;
while (!mypq.empty())
{
cout << mypq.top() << " ";
mypq.pop();
}
cout << endl;
}
int main()
{
int i;
while (true)
{
menu();
cout << “请输入菜单号:” << endl;
cin >> i;
if (i == 8)break;
switch (i)
{
case 1:Push_pq(); break;
case 2:Pop_pq(); break;
case 3:Insert(); break;
case 4:Top(); break;
case 5:Swap(); break;

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注网络安全)
img

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

。**

需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注网络安全)
[外链图片转存中…(img-kBxPhXRv-1713303608561)]

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 20
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值