1 #include<iostream> 2 #include<queue> 3 using namespace std; 4 struct tree{ 5 int num; 6 string s; 7 tree(int x,string zfc) 8 { 9 num=x; 10 s=zfc; 11 } 12 friend bool operator>(const tree &a,const tree &b) 13 { 14 return a.num> b.num; 15 } 16 / * is obviously not used to override the UF is not the same element drops * / . 17 / * BOOL operator <(const A Tree &) const 18 is { . 19 return a.num> NUM; 20 is } * / 21 is BOOL operator == ( const Tree & a) const 22 is { 23 is return a.num> NUM; 24 } 25 }; 26 is / * two overloaded <, one can easily apparent weight inside and outside The number of different parameters contained drops * / 27 BOOL operator <(A Tree, Tree B) 28 { 29 return AS < BS; 30 } 31 is int main () 32 { 33 is The priority_queue <Tree> Q; 34 is / * as to use the priority queue, it is necessary to reload < * / 35 Tree T ( . 5 , " HH " ); 36 q.push (T); 37 [ Tree R & lt ( . 4 , " XX " ); 38 is q.push (R & lt); 39 the while (! q.empty ()) 40 { 41 is cout<<q.top().num<<" "<<q.top().s<<endl; 42 q.pop(); 43 } 44 if(r==t) 45 cout<<"hh"; 46 else 47 cout<<"xx"; 48 }
This is the normal priority queue installed inside a custom type.
1 #include<iostream> 2 #include<queue> 3 using namespace std; 4 typedef struct tree* T; 5 struct tree{ 6 int num; 7 string s; 8 T next; 9 tree(){ 10 } 11 tree(int x,string zfc) 12 { 13 num=x; 14 s=zfc; 15 } 16 friend BOOL operator <( const Tree A, const Tree B) . 17 { 18 is return b.num> a.num; . 19 } 20 is }; 21 is struct CMP { 22 is BOOL operator () (T A, T B) 23 is { 24 return A -> NUM> B-> NUM; 25 } 26 is }; / * use the sort function carrier body structure, priority_queue the three parameters must be written * / 27 int main () 28 { 29 // the priority_queue <T, Vector <T>, cmp> q; 30 The priority_queue <T> Q; / * do not know why, to create a priority queue where there is no overload ranking function, it not being given a * / 31 is T = T new new Tree (); / * pointer type must allocate space * / 32 T-> NUM = . 5 ; 33 is T-> S = " HHH " ; 34 is q.push (T); 35 T = R & lt new new Tree (); 36 R-> NUM = . 4 ; 37 [ R-> = S " XXX " ; 38 is q.push (R & lt); 39 T H = new new Tree ( . 3 ,"nn"); 40 q.push(h); 41 while(!q.empty()) 42 { 43 cout<<q.top()->num<<" "<<q.top()->s<<endl; 44 q.pop(); 45 } 46 }
If using three parameters priority_queue, necessary structural body carrying a sort function. Then use the process, encountered some problems, I do not know why.