473,382 Members | 1,665 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

priority_queue predicate

priority_queue usually uses the greater<intpredicate 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,
cmphp;" thing.

My question is how should I write the "cmp" function?

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.

Any comments? Thanks in advance.
Oct 3 '08 #1
8 4140
On 2008-10-03 12:05:38 -0400, thomas <Fr*********@gmail.comsaid:
priority_queue usually uses the greater<intpredicate 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,
cmphp;" 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.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Oct 3 '08 #2
On 2008-10-03 12:41:16 -0400, Pete Becker <pe**@versatilecoding.comsaid:
On 2008-10-03 12:05:38 -0400, thomas <Fr*********@gmail.comsaid:
>priority_queue usually uses the greater<intpredicate 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,
cmphp;" 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)

Oct 3 '08 #3
On Oct 4, 12:44*am, Pete Becker <p...@versatilecoding.comwrote:
On 2008-10-03 12:41:16 -0400, Pete Becker <p...@versatilecoding.comsaid:


On 2008-10-03 12:05:38 -0400, thomas <FreshTho...@gmail.comsaid:
priority_queue usually uses the greater<intpredicate 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,
cmphp;" 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::priority_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::priority_queue' : no appropriate
default constructor available
------------------------
Oct 3 '08 #4
On Oct 4, 1:31*am, thomas <FreshTho...@gmail.comwrote:
On Oct 4, 12:44*am, Pete Becker <p...@versatilecoding.comwrote:


On 2008-10-03 12:41:16 -0400, Pete Becker <p...@versatilecoding.comsaid:
On 2008-10-03 12:05:38 -0400, thomas <FreshTho...@gmail.comsaid:
>priority_queue usually uses the greater<intpredicate 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,
>cmphp;" 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::priority_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::priority_queue' : no appropriate
default constructor available
------------------------- 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;

#define PII pair<int,int>
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(){

}
---------------
this one in case you don't know what PII is.
Oct 3 '08 #5
On 2008-10-03 13:33:55 -0400, thomas <Fr*********@gmail.comsaid:
On Oct 4, 1:31Â*am, thomas <FreshTho...@gmail.comwrote:
>On Oct 4, 12:44Â*am, Pete Becker <p...@versatilecoding.comwrote:


>>On 2008-10-03 12:41:16 -0400, Pete Becker <p...@versatilecoding.comsa
id:
>>
>>>On 2008-10-03 12:05:38 -0400, thomas <FreshTho...@gmail.comsaid:
>>>>priority_queue usually uses the greater<intpredicate function.
>>>>But as you know, we don't always use priority_queue<int>. Actually w
e
>>>>may need the "priority_queue<pair<int,int>, vector<pair<int,int,
cmphp;" 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" predicat
e.
>>
>>>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::priority_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::priority_queue' : no appropriate
default constructor available
------------------------- 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;

#define PII pair<int,int>
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(){

}
---------------
this one in case you don't know what PII is.
Presumably the result of compiling this code was different from the
result shown in the previous message.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Oct 3 '08 #6
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<cstdlib>
#include<cmath>
#include<list>
#include<stack>
#include<queue>
The headers required are queue, utility and vector.

using namespace std;

#define PII pair<int,int>
A typedef is preferable. Two of them would be useful.
typedef std::pair<int,intPII;
typedef std::pair<PII,intelement;
bool cmp(const pair<PII,int&x, const pair<PII,int&y){
return x.second < y.second;
}
Comparisons can be done on second but first would be more akin to the
associative containers.
priority_queue<pair<PII, int>, vector<pair<PII,int, cmp>
The third argument must be a type. It must be this: bool (*)(element
const &x, element const &y).
heap; //pair<pair<node I, node j>, weight>
A constructor that sets the comparison function must be invoked i.e.
heap(cmp)

A functor is easier to use than a function when you know how to use
them. I would rewrite the code to use a functor and to sort on first
instead of second.

Fraser.
Oct 4 '08 #7
On 3 Oct, 17:05, thomas <FreshTho...@gmail.comwrote:
priority_queue usually uses the greater<intpredicate 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,
cmphp;" thing.

My question is how should I write the "cmp" function?

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.
std::sort() accepts the predicate by value and deduces its type, so
both function pointers and function objects (with operator()) work.

std::priority_queue<>, on the other hand, does not deduce the template
types, so that you have to specify it explicitly. The type of your
predicate is a function pointer:

typedef priority_queue<
pair<int,int>
, vector<pair<int,int
, bool(*)(pair<int,int>&, pair<int,int>&) // <--- here
Q;
Constructors of the queue use a default-initialised value for the
predicate, which in your case is a NULL function pointer. Obviously,
you need to pass a pointer to you predicate function explicitly when
constructing the queue:

Q q(cmp);

It may be a bit easier to make your predicate a class, so that you
don't have to pass it into constructors:

struct cmp2
{
bool operator()(pair<int,intconst&, pair<int,intconst&)
const;
};

typedef priority_queue<
pair<int,int>
, vector<pair<int,int
, cmp2
Q2;
Q2 q2; // <--- default-initialised cmp2 works

Please also note, that when you use function pointers the calls
through a function pointer can not be inlined. With function objects
they can.

--
Max
Oct 4 '08 #8
ra*@zedat.fu-berlin.de (Stefan Ram) kirjutas:
"Fraser Ross" <z@zzzzzz.comwrites:
>>>>#include<stack>
#include<queue>
The headers required are queue, utility and vector.

In a class, I explained that using certain names and
operators requires certain include directives.

Then I was asked if it would be possible to always
copy /all/ standard include directives to the start
of a program. I answered that no one does this, but
that it would be possible.

Is there a reason not to do so (except for a possible
increase in compilation time)?
Actually, a similar approach can be used for *reducing* compile times in
certain implementations (e.g. MSVC++). The idea is that you gather most
includes, including all needed standard includes, into a single header,
which is then marked to be precompiled. All cpp files include this header
first. As this header will be compiled only once it often means
significantly faster compile times for large projects.

As it might be quite hard to guess which standard includes any cpp might
need in an evolving project, I can imagine that it would be sometimes
practical to put *all* standard includes in the precompiled file, just in
case. Never done that myself, though.

hth
Paavo
Oct 4 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Dave | last post by:
Hello all, I'm pondering why the default underlying container for std::priority_queue<> is std::vector<>. It would seem that inserts are liable to happen anywhere, which would make std::list<>...
3
by: Tino | last post by:
In using std::priority_queue, I'm concerned about the expense of memory allocation and copying as the priority_queue grows large. std::vector has reserve() to address this concern, though there...
9
by: Rich S. | last post by:
Hi In an earlier posting I was asking how to read thru millions of data records keeping the top 2000 (where the top values are based upon a field in the record) and niklasb suggested using a...
3
by: zl2k | last post by:
hi, all Here is what I want to do: to wrap my self defined class in a shared_ptr and then insert it into the priority_queue. The priority_queue should pump the least element of the self defined...
9
by: Henning Hasemann | last post by:
I'm using a stl-priority queue and want - find out if a certain item is contained in the queue - to be able iterate over all items without having to pop() them, order does not matter. I couldnt...
18
by: J.M. | last post by:
I would like to use a data structure (e.g. from the STL) that always allows me to retrieve the largest element. (I want to push in elements, and remove the largest, push in further elements, etc.)...
6
by: Eric Lilja | last post by:
Hello, I have a the following priority_queue: priority_queue<pair<int, string pq; AFAICT, priority_queues doesn't support iterators. My question: is there a way to print its contents without...
24
by: Joe, G.I. | last post by:
Can anyone help me w/ a priority_queue. I'm generating MyEvent classes and I put them on a priority_queue, but I don't know how to get them in priority. The priority is the event w/ the smallest...
5
card
by: card | last post by:
I was wondering if anyone has a definitive answer on whether the STL priority_queue is dynamic? What I mean by this is best illustrated by a simple example. Suppose I have a priority queue of class...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.