Does changing a priority queue element result in resorting the queue?(更改优先级队列元素是否会导致重新使用队列?)
问题描述
我有一个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.
这篇关于更改优先级队列元素是否会导致重新使用队列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:更改优先级队列元素是否会导致重新使用队列?
基础教程推荐
- 初始化列表*参数*评估顺序 2021-01-01
- CString 到 char* 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
