On Oct 4, 12:44*am, Pete Becker <p...@versatilecoding.com> wrote:
> On 2008-10-03 12:41:16 -0400, Pete Becker <p...@versatilecoding.com> said:
>
>
>
>
>
> > On 2008-10-03 12:05:38 -0400, thomas <FreshTho...@gmail.com> said:
>
> >> priority_queue usually uses the greater<int> predicate function.
>
> >> But as you know, we don't always use priority_queue<int>. Actually we
> >> may need the "priority_queue<pair<int,int>, vector<pair<int,int> >,
> >> cmp> hp;" thing.
>
> >> My question is how should I write the "cmp" function?
>
> > It depends on what you want it to do.
>
> >> I tried this one:
>
> >> bool cmp(pair<int,int> &x, pair<int,int> &y){
> >> * * return x.second < y.second;
> >> }
>
> >> but it doesn't work while it usually makes sense for "sort" predicate.
>
> > It should work just fine, if you want your elements sorted by their
> > second field. If that's not what you want, then you need a different
> > comparison function.
>
> However, it should probably take its arguments by const reference or by
> value. But since you haven't posted real code, nor provided any details
> about what "doesn't work" means, it's not possible to tell what, if
> anything, is wrong.
>
> --
> * Pete
> Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
> Standard C++ Library Extensions: a Tutorial and Reference
> (www.petebecker.com/tr1book)- Hide quoted text -
>
> - Show quoted text -
-------code-----
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<cstdlib>
#include<cmath>
#include<list>
#include<stack>
#include<queue>
using namespace std;
bool cmp(const pair<PII,int> &x, const pair<PII,int> &y){
return x.second < y.second;
}
priority_queue<pair<PII, int>, vector<pair<PII,int> >, cmp>
heap; //pair<pair<node I, node j>, weight>
int main(){
}
---------end---------
for simplicity, you can compile the code above.
I'm using vc8, and got the errors:
----------------------
------ Build started: Project: pku, Configuration: Debug Win32 ------
Compiling...
a.cpp
...\a.cpp(14) : error C2065: 'PII' : undeclared identifier
...\a.cpp(17) : error C3203: 'pair' : unspecialized class template
can't be used as a template argument for template parameter '_Ty',
expected a real type
...\a.cpp(17) : error C3203: 'pair' : unspecialized class template
can't be used as a template argument for template parameter '_Ty',
expected a real type
...\a.cpp(17) : error C2923: 'std:

riority_queue' : 'cmp' is not a
valid template type argument for parameter '_Pr'
..\a.cpp(14) : see declaration of 'cmp'
...\a.cpp(17) : error C2133: 'heap' : unknown size
...\a.cpp(17) : error C2512: 'std:

riority_queue' : no appropriate
default constructor available
------------------------