for pointers you must do the following(2 modifications that you must do): include<iostream> #include<vector> #include<queue> using namespace std; int j; class vertex { public: vertex *parent; int value; vertex() { parent = NULL; value=++j; } ~vertex(); }; //define your own comparison predicate: struct vertex_compare : binary_function<vertex*, vertex*, bool> { bool operator()(const vertex* v1, const vertex* v2) const { return (v1->value > v2->value); } };
int main() { int i; vertex *array; //and now use this comparison predicate priority_queue<vertex *,vector<vertex *>,vertex_compare > Q; //now can insert your elements of type vertex in pq for(i=0;i<9;++i) { array = new vertex; Q.push(array);} //now Q.top() will give the minimum value while(!Q.empty()){ cout<<endl<<Q.top()->value; Q.pop();} cout<<endl<<endl; system("PAUSE"); } On Sun, Jun 15, 2008 at 1:42 PM, Pratyush <[EMAIL PROTECTED]> wrote: > > Thankyou Shaarang this helped me a lot and raised another doubt. I > think you can help me further... > instead of the array of object I now used class pointers and created > some objects dynamically. > I modified the PQ decalration to hold pointers but i get an unexpected > result.. > here's what i did > > #include<iostream> > #include<vector> > #include<queue> > using namespace std; > int j; > class vertex > { > public: > vertex *parent; > int value; > vertex() > { > parent = NULL; > value=++j; > } > ~vertex(); > }; > > bool operator < (const vertex& v1, const vertex& v2) > { > return (v1.value > v2.value); > } > > int main() > { > int i; > vertex *array; > //create a priority_queue > priority_queue<vertex *,vector<vertex *>,less<vector<vertex > *>::value_type> >Q; > > //now can insert your elements of type vertex in pq > for(i=0;i<9;++i) > { array = new vertex; > Q.push(array);} > //now Q.top() will give the minimum value > while(!Q.empty()){ > cout<<endl<<Q.top()->value; > Q.pop();} > > cout<<endl<<endl; > > system("PAUSE"); > } > > > But I got the result as.. > > 9 > 8 > 7 > 6 > 5 > 4 > 3 > 1 > 2 > > Press any key to continue . . . > > I couldnt explain this. How can I do the above this way (pointer way > without array). > > On Jun 14, 5:15 am, "shaarang tyagi" <[EMAIL PROTECTED]> wrote: > > you must do the following: > > > > class vertex > > { > > public: > > vertex *parent; > > int value; > > vertex() > > { > > parent = NULL; > > value = rand(); > > } > > ~vertex(); > > }Array[10]; > > > > bool operator < (const vertex& v1, const vertex& v2) > > { > > return (v1.value > v2.value);} > > > > vo > id main() > > { > > //create a priority_queue > > > > > priority_queue<vertex,vector<vertex>,less<vector<vertex>::value_type> > > > > > Q; > > > > //now can insert your elements of type vertex in pq > > for(i=0;i<9;++i) > > Q.push(Array[i]); > > //now Q.top() will give the minimum value > > cout<<Q.top().value; > > > > } > > On Sat, Jun 14, 2008 at 3:40 PM, Pratyush <[EMAIL PROTECTED]> > wrote: > > > > > I want to construct a min priority queue to hold class objects > > > eg > > > class vertex > > > { > > > public: > > > vertex *parent; > > > int value; > > > vertex() > > > { > > > parent = NULL; > > > value = rand(); > > > } > > > ~vertex(); > > > }array[10]; > > > > > priority_queue<vertex> Q; > > > for(i=0;i<9;++i)Q.push(array[i]); > > > > > the min-heap is to be ordered by the "value" and Q.top() should return > > > the vertex with min value ( like we require in prim's algorithm). > > > Can anybody assist me how to create the priority_queue using STL in C+ > > > + and how is the comparator class and container is to be written for > > > this. > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/algogeeks -~----------~----~----~----~------~----~------~--~---