简体   繁体   English

修改的C ++的priority_queue比较器无法正常工作

[英]Modified comparator for priority_queue of C++ not working properly

In the code given below, I am trying to keep some values in priority_queue sorted accordingly their key values, which are stored in the "key" vector. 在下面给出的代码中,我试图将priority_queue中的某些值保持相应的键值排序,这些键值存储在“键”向量中。 And then, I am changing the key values to see if the comparator working properly or not. 然后,我正在更改键值,以查看比较器是否正常工作。 Here, when I change the key[8] to 10, value 8 changes its position in the queue correctly. 在这里,当我将key [8]更改为10时,值8会正确更改其在队列中的位置。 But when I changed the key[2] to -1, it changed it's position in the queue, but not correctly. 但是,当我将key [2]更改为-1时,它更改了它在队列中的位置,但没有正确。 It should be positioned at the top of the queue, as it's key value is the smallest, but it's not. 它应该位于队列的顶部,因为它的键值是最小的,但不是。

Is my way of writing the code of the comparator wrong? 我编写比较器代码的方式是否错误? Or, It's something else that I am not doing correctly? 或者,这是我做不正确的事情吗?

I want to know the correct way to modify the comparator of priority queue if I want to sort values in ascending order accordingly to their key values. 我想知道正确的方法来修改优先级队列的比较器,如果我想根据其键值对值进行升序排序。

在此处输入图片说明

#include <bits/stdc++.h>
using namespace std;
vector <int> key(1000);
struct comp{
    bool operator()(int a,int b)const{
        return key[a]>key[b];
    }
};
int main()
{
    priority_queue <int,vector<int>,comp> q,temp;
    for(int a=0;a<10;a++){
        int n=rand()%16;
        key[a]=n;
        q.push(a);
    }
    while(!q.empty()){
        temp=q;
        while(!temp.empty()){
            cout<< temp.top() << "(" << key[temp.top()] << ") ";
            temp.pop();
        }
        cout<<endl<<endl;
        int u,v;
        cin>> u >> v;
        key[u]=v;
    }
    return 0;
}

You never changed q. 您从未改变过。 You made a copy of q in temp but never did anything to q. 您在temp制作了q的副本,但从未对q做任何事情。

The intent and the code are reasonable enough, but the issue is much more fundamental. 目的和代码足够合理,但是问题更根本。 The problem is that std::priority_queue does not support decrease key operation out-of-the-box. 问题在于std::priority_queue不支持立即减少键操作。

In order to perform such operations, you would need to manually maintain the list of keys and call std::make_heap , std::push_heap from <algorithm> . 为了执行此类操作,您需要手动维护键列表,并从<algorithm>调用std::make_heapstd::push_heap

See this for a more detailed answer: https://stackoverflow.com/a/9210662/1045285 请参阅此以获得更详细的答案: https : //stackoverflow.com/a/9210662/1045285

As a side note, consider instead including: 作为旁注,请考虑包括以下内容:

#include <priority_queue>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++如何使用用户定义的比较器正确地将priority_queue作为属性添加到类? - C++ How to properly add a priority_queue to a class as an attribute with a user-defined comparator? 未在范围中声明-Priority_queue C ++的好友比较器类 - Not declared in scope - friend comparator class for priority_queue C++ C++:struct和decltype比较器的priority_queue - c++: priority_queue of struct and decltype comparator priority_queue c++ 带有自定义比较器和 O(n) 时间 - priority_queue c++ with custom comparator and O(n) time 具有自定义字符串比较器的字符串的priority_queue的C ++向量 - C++ vector of priority_queue of strings with custom strings comparator 为 priority_queue 使用自定义比较器会增加时间 C++ - Using custom comparator for priority_queue increases time C++ C++ priority_queue,带有自定义比较器并删除任何项目 - C++ priority_queue with custom comparator and removal of any item 使用带有lambda比较器错误的映射的C ++ priority_queue - C++ priority_queue using map with lambda comparator error C ++中std :: priority_queue的比较器部分的含义是什么? - What is the meaning of the comparator part of the std::priority_queue in C++? C++ priority_queue 与 lambda 比较器错误 - C++ priority_queue with lambda comparator error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM