更改优先级队列元素是否会导致重新排序队列? [英] Does changing a priority queue element result in resorting the queue?

查看:392
本文介绍了更改优先级队列元素是否会导致重新排序队列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个priority_queue,我想修改它的某些内容(优先级值),那么队列是否会被重新排序?

I have a priority_queue, and I want to modify some of it's contents (the priority value), will the queue be resorted then?

这取决于是否重新排序在push / pop上(更可能是因为您只需要插入,而不是整体使用),或者在访问top或pop时。

It depends if it resorts on push/pop (more probable, becouse you just need to "insert", not resort whole), or when accessing top or pop.

我真的想更改一些内容队列中的元素。这样的事情:

I really want to change some elements in the queue. Something like that:

priority_queue<int> q;

int a=2,b=3,c=5;
int *ca=&a, *cb=&b, cc=&c;

q.push(a);
q.push(b);
q.push(c); //q is now {2,3,5}

*ca=4;

//what happens to q?
// 1) {3,4,5}
// 2) {4,2,5}
// 3) crash


推荐答案

priority_queue 复制您推送到其中的值。最后,您的分配对优先级队列的顺序或其中存储的值的影响为零。

priority_queue copies the values you push into it. Your assignment at the end there will have zero effect on the order of the priority queue, nor the values stored inside of it.

这篇关于更改优先级队列元素是否会导致重新排序队列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆