priority-queue

Why does my PriorityBlockingQueue in Java not sort properly?

喜夏-厌秋 提交于 2019-12-20 04:19:50
问题 For some reason when I add to the priority queue, it doesn't sort my strings entirely alphabetically and I can't see why. This is the code which adds to the PriorityBlockingQueue: String toAdd = String.format("%s/%s", directory, s); outputData.add(toAdd); But I get not entirely sorted output (only first few lines but you can see it's not sorted): ../StartingTree/files/abknl/apfmpohgyh/a.class ../StartingTree/files/abknl/apfmpohgyh/a.java ../StartingTree/files/abknl/aqybc/aeph.java ..

How to sort a multidimensional vector of floats?

吃可爱长大的小学妹 提交于 2019-12-20 03:49:33
问题 So, I have a set of points in 3D, and I would like to store them in a 3 dimensional vector. Then I need sort that vector, giving priority first to the X dimention, then Y, then Z. So, for example, if I have this set of points: P1 = (5, 10 ,9) P2 = (1, 11, 4) P3 = (8, 5, 2) P4 = (5, 10, 3) P5 = (5, 4, 0) I would like to get a vector sorted like this: [1, 11, 4] [5, 4, 0] [5, 10, 3] [5, 10, 9] [8, 5, 2] So, how can a sort a multidimentional vector taking all rows into account? Should I use std:

Java: PriorityQueue initializations

杀马特。学长 韩版系。学妹 提交于 2019-12-20 01:49:30
问题 I am trying to understand the following line which initiates a Priority Queue: PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> b[1] - a[1]); Comparing with Constructor section in the document, https://docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html I couldn't figure out which Constructor it uses. Could someone please share the thought? Also, is there a document that could better explain/define syntax (a, b) -> b[1] - a[1] ... though I could guess what it means. Thanks a

Java : Priority Queue

梦想的初衷 提交于 2019-12-19 17:38:25
问题 I have a java program which goes like this public class PriorityQueueExample { public static void main(String[] args) { PriorityQueue<Integer> pq = new PriorityQueue<Integer>(); pq.add(10); pq.add(1); pq.add(9); pq.add(2); pq.add(8); pq.add(3); pq.add(7); pq.add(4); pq.add(6); pq.add(5); System.out.println(pq); } } My Question is why does not the priority queue sort them. As per the java specs it implements comparable and maintains the sorting order(natural sorting) My output of the program

Java: Access local variables from anon inner class? (PriorityQueue)

允我心安 提交于 2019-12-19 10:07:53
问题 I want to use a PriorityQueue to do a topological sort on a graph. For brevity, I'd like to use an anonymous inner class for the comparator. However, I need access to the graph g in order to determine the in degree of the nodes I'm looking at. Is this possible? /** * topological sort * @param g must be a dag */ public static Queue<String> topoSort(DirectedGraph<String, DefaultEdge> g) { Queue<String> result = new PriorityQueue<String>(g.vertexSet().size(), new Comparator<String>() {

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

落花浮王杯 提交于 2019-12-19 05:50:26
问题 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

STL priority queue and overloading with pointers

元气小坏坏 提交于 2019-12-19 03:13:10
问题 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

STL Priority Queue - deleting an item

泪湿孤枕 提交于 2019-12-18 12:58:23
问题 I want to implement a timer queuing system using the C++ STL priority_queue container adapter. My problem is that I want to occasionally cancel a timer, however there are no interfaces that enable me to easily delete an item in the priority_queue that is not the top item. Any suggestions?. Thank you for your help. 回答1: I had the exact same scenario once and did the following: the structure I kept in std::priority_queue contained only the time to sort by and an index to a std::vector<Handler>

When should I use make_heap vs. Priority Queue?

纵饮孤独 提交于 2019-12-18 12:07:33
问题 I have a vector that I want to use to create a heap. I'm not sure if I should use the C++ make_heap function or put my vector in a priority queue? Which is better in terms of performance? When should I use one vs. the other? 回答1: There's no difference in therms of performance. std::priority_queue is just an adapter class that wraps the container and the very same heap-related function calls into a class. The specification of the std::priority_queue openly states that. By building a heap-based

How to remove element not at top from priority_queue?

你。 提交于 2019-12-18 10:43:34
问题 In my program I need to delete an element from a priority queue that is not at the top. Can that be done? If not, please suggest a way to do so except creating your own heap. 回答1: The standard priority_queue<T> can be customized through inheritance. It has protected members c and comp that can be referenced in a descendant class. template<typename T> class custom_priority_queue : public std::priority_queue<T, std::vector<T>> { public: bool remove(const T& value) { auto it = std::find(this->c