priority-queue

How to implement sorting method for a c++ priority_queue with pointers

只谈情不闲聊 提交于 2019-12-01 03:33:39
My priority queue declared as: std::priority_queue<*MyClass> queue; class MyClass { bool operator<( const MyClass* m ) const; } is not sorting the items in the queue. What is wrong? I would not like to implement a different (Compare) class. Answer summary: The problem is, the pointer addresses are sorted. The only way to avoid this is a class that 'compares the pointers'. Now implemented as: std::priority_queue<*MyClass, vector<*MyClass>, MyClass::CompStr > queue; class MyClass { struct CompStr { bool operator()(MyClass* m1, MyClass* m2); } } Give the que the Compare functor ptr_less. If you

Simple Java PriorityQueue<String> error

不羁岁月 提交于 2019-12-01 02:52:54
问题 All I am doing is add three strings to a Java PriorityQueue and then print them out This is my code: import java.util.*; import java.lang.*; class Main { public static void main (String[] args) throws java.lang.Exception { PriorityQueue<String> pq=new PriorityQueue<String>(); pq.add("abc"); pq.add("ability"); pq.add("aberdeen"); String s="ability"; System.out.println(s.compareTo("aberdeen")); System.out.println(pq); } } And this is the output: 4 [abc, ability, aberdeen] Shouldn't this be abc,

PriorityQueue: Student cannot be cast to java.lang.Comparable

不问归期 提交于 2019-11-30 21:32:02
问题 I'm using the PriorityQueue structure to get some fields set by the user, here is part of the code: package gqg; import java.util.Queue; public class Student { //variables (ID, Name, ...), constructors, getters and setters... Queue<Student> StudentQueue = new PriorityQueue<Student>(); public void Add() { //method to add the student at Queue for(int x=0; x<1; x++) { Student st = new Student(); System.out.println("+---------------------------+\n" + "| Students Registration |\n" + "+------------

STL priority queue and overloading with pointers

偶尔善良 提交于 2019-11-30 20:49:12
This is my first time using a priority queue. I'm trying to implement Dijkstra's algorithm for school and I figured I need a min heap to do this. Right now my nodes are pointers and I want to compare their weight, but I don't think I can overload > and < with pointers? Is there a way I could accomplish this? Code this far: priority_queue<Node*, vector<Node*>, node_comparison> minHeap; And then I have a struct to compare the node's weights struct node_comparison { bool operator<( const Node* a, const Node* b ) const { return a->totalWeight < b->totalWeight; } }; However it says there are too

Dijkstra algorithm with min-priority queue

蹲街弑〆低调 提交于 2019-11-30 18:10:02
I'm trying to implement the dijkstra algorithm with priority queue, but I can't understand how it works. I read many guide on the web but I can't understand this algorithm at all. My question are: what is the priority for each node? I think that it is the weight of the incoming edge with minimun value, but I'm not sure. Is this true? Second question, when I extract the root of the queue, how works if this node is not adjacency with no one of the visited nodes? You should use priority queue where the vertex with the shortest distance from the starting vertex will get the highest priority.

How to preserve the order of elements of the same priority in a priority queue implemented as binary heap?

别来无恙 提交于 2019-11-30 17:54:45
I have created a binary heap, which represents a priority queue. It's just classical well known algorithm. This heap schedules a chronological sequence of different events ( the sort key is time ). It supports 2 operation: Insert and Remove. Each node's key of the heap is greater than or equal to each of its children. However, adding events with the same key doesn't preserve the order they were added, because each time after Remove or Insert were called, the heap-up and the heap-down procedures break the order. My question is: what should be changed in a classical algorithm to preserve the

How to update elements within a heap? (priority queue)

瘦欲@ 提交于 2019-11-30 14:59:22
问题 When using a min/max-heap algorithm, priorities may change. One way to handle this is to removal and insert the element to update the queue order. For priority queues implemented using arrays, this can be a performance bottleneck that seems avoidable, especially for cases when the change to priority is small. Even if this is not a standard operation for a priority queue, this is a custom implementation which can be modified for my needs. Are there well known best-practice methods for updating

Does changing a priority queue element result in resorting the queue?

微笑、不失礼 提交于 2019-11-30 13:54:32
I have a priority_queue, and I want to modify some of it's contents (the priority value), will the queue be resorted then? 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 copies the values you push into it. Your assignment at the end

Android how to read multiple BLE characteristics with a PriorityQueue

做~自己de王妃 提交于 2019-11-30 13:22:57
问题 A bit stuck here, might need your help. I want to read several BLE characteristics at once, some people suggest using PriorityQueue for that. I already know all the uuids, etc. just need a way to read several at once. Could anyone explain how exactly should it look like? Or maybe there is yet another easier solution? Thanks in advance, here is my code: public static final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() { PriorityQueue<BluetoothGattCharacteristic> queue = new

Bounded PriorityBlockingQueue

。_饼干妹妹 提交于 2019-11-30 11:18:52
PriorityBlockingQueue is unbounded, but I need to bound it somehow. What is the best way to achieve that? For information, the bounded PriorityBlockingQueue will be used in a ThreadPoolExecutor . NB: By bounded I don't want to throw Exception if that happens, I want to put the object in the queue and then cut it based on its priority value. Is there any good way to do this cut thingie? I actually wouldn't subclass it. While I can't put together example code right now, I'd suggest a version of the decorator pattern. Create a new class and implement the interfaces implemented by your class of