Thursday 15 March 2012

c++ - STL priority queue and overloading with pointers -


This is my first time using the priority queue. I am trying to implement Djextra's algorithm for school and I felt that I needed a minimum pile to do this. Right now my nodes are pointers and I want to compare their weight, but I do not think I have overloaded & gt; And & lt; With indicators? Is there any way I can accomplish this?

Remove this code:

  priority_key < Node *, vector and lieutenant; Node *>, node_comparion & gt; MinHeap;   

And then I have a structure to compare the weight of the node

  struct node_comparison {bool operator & lt; (Const node * a, const node * b) const {returns a-> Kulwait & lt; B & gt; TotalWeight; }};   

Although it says that there are too many parameters for this operator function. I was trying to explain how I could manage a minimum heap preference queue with my nodes for a while and was stuck. Any ideas?

If I understand your question correctly, then I believe what you really want node_commerce A Factor (more specifically, a binary pocket):

  struct node_comparison {bool operator (cn node * a, cant node * b) CONST {Return to A-> Kulweit & lt; B & gt; TotalWeight; }}; A fun physician is a class whose object provides the surcharge of the call operator ( operator) ), and therefore, can be implemented with the same syntax to apply a function: < / P> 
  node * p1 = ...; Node * p2 = ...; Node_compression vibrate; Bool res = comp (p1, p2) // & lt; == Adds your overload off operator ()   

internally, std :: priority_queue Your interpretation is more or less like I snippet the code above, And implemented that method to compare between its elements.


The advantage of the fonts on regular tasks is that they position information (some you probably will not need for the moment, but which are often desirable):

  #include & lt; Cmath & gt; Struct my_comparator {my_comparator (int x): _x (x) {} bool operator () (int n, int m) const {return abs (n - _x) & lt; Stomach (m - _x); } Int _x; };   

The above-mentioned determination, for example, compares to the integer how far they are from the second integer given at construction time. How can this be done:

  #include & lt; Queue & gt; # Include & lt; Iostream & gt; Void foo (int pivot) {my_comparator mc (smoke); Std :: priority_queue & lt; Int, std :: deque & lt; Int>, my_comparator & gt; Pq (mc); Pq.push (9); Pq.push (2); Pq.push (17); While (! Pq.empty ()) {std :: cout & lt; & Lt; Pq.top (); Pq.pop (); }} Int main () {foo (7); Std :: cout & lt; & Lt; Std :: endl; Foo (10); }    

No comments:

Post a Comment