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

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

问题描述

我有一个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天全站免登陆