473,804 Members | 3,932 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

emptying a priority_queue efficiently

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.) It seems a priority_queue from
the STL would work fine. However at some point, I am finished (even though
the queue is not empty) and want to throw away the rest of the elements. It
seems, I have to do this using:

while (!Q.empty()) Q.pop();

Other classes offer a resize() or a clear() and my guess that this is much
more efficient than using a while-loop. However, these functions are not
available for a priority_queue. Any ideas? Thanks in advance for any help.

Jan
Feb 6 '07 #1
18 5533
* J.M.:
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.) It seems a priority_queue from
the STL would work fine. However at some point, I am finished (even though
the queue is not empty) and want to throw away the rest of the elements. It
seems, I have to do this using:

while (!Q.empty()) Q.pop();

Other classes offer a resize() or a clear() and my guess that this is much
more efficient than using a while-loop. However, these functions are not
available for a priority_queue. Any ideas? Thanks in advance for any help.
template< typename T >
void makeEmpty( std::priority_q ueue<T>& q )
{
std::priority_q ueue<Tempty;
std::swap( q, empty );
}

Of course, you need to measure measure measure.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 6 '07 #2
J.M. escreveu:
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.) It seems a priority_queue from
the STL would work fine. However at some point, I am finished (even though
the queue is not empty) and want to throw away the rest of the elements. It
seems, I have to do this using:

while (!Q.empty()) Q.pop();

Other classes offer a resize() or a clear() and my guess that this is much
more efficient than using a while-loop. However, these functions are not
available for a priority_queue. Any ideas? Thanks in advance for any help.
I might be missing something, but since you always wish to obtain the
largest element, I guess a set and an operator< for your element type
would give you what you need. You might use rbegin() or begin() to get
to the last element, depending on whether your operator< sorts elements
in ascending or descending order, respectivelly.

Regards,

--
Ney André de Mello Zunino
http://blog.zunino.eti.br/
Feb 6 '07 #3
J.M. <jm************ ***@gmx.dewrote :
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.) It seems a priority_queue from
the STL would work fine. However at some point, I am finished (even though
the queue is not empty) and want to throw away the rest of the elements. It
seems, I have to do this using:

while (!Q.empty()) Q.pop();

Other classes offer a resize() or a clear() and my guess that this is much
more efficient than using a while-loop. However, these functions are not
available for a priority_queue. Any ideas? Thanks in advance for any help.
Alf P. Steinbach gave you a good answer. Another idea is to use a
different container and call std::make_heap( ) on it, but consistently
maintaining the heap property may be less efficient than just using the
priority queue.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Feb 6 '07 #4
Marcus Kwok <ri******@gehen nom.invalidwrot e:
J.M. <jm************ ***@gmx.dewrote :
>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.) It seems a priority_queue from
the STL would work fine. However at some point, I am finished (even though
the queue is not empty) and want to throw away the rest of the elements. It
seems, I have to do this using:

while (!Q.empty()) Q.pop();

Other classes offer a resize() or a clear() and my guess that this is much
more efficient than using a while-loop. However, these functions are not
available for a priority_queue. Any ideas? Thanks in advance for any help.

Alf P. Steinbach gave you a good answer. Another idea is to use a
different container and call std::make_heap( ) on it, but consistently
maintaining the heap property may be less efficient than just using the
priority queue.
After more investigation, <algorithmals o offers std::push_heap( ) and
std::pop_heap() in addition to std::make_heap( ), so maintaining the heap
property may not be as big of a hit as I thought. Of course, you will
need to measure for yourself if the performance is acceptable.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Feb 6 '07 #5
J.M. wrote:
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.) It seems a priority_queue from
the STL would work fine. However at some point, I am finished (even though
the queue is not empty) and want to throw away the rest of the elements. It
seems, I have to do this using:

while (!Q.empty()) Q.pop();

Other classes offer a resize() or a clear() and my guess that this is much
more efficient than using a while-loop. However, these functions are not
available for a priority_queue. Any ideas? Thanks in advance for any help.

Jan
Besides previously given answers... you could design things so that the
priority_queue goes out of scope when you're done with it or, if that's
not possible, dynamically allocate it and attach it to a smart pointer
which you can manually delete as soon as convenient. (Implicit in this
is also the option to simply do nothing and wait for the queue to fall
out of scope, but I gather that this is not sufficient for you.)
Feb 6 '07 #6
Mark P schrieb:
J.M. wrote:
>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.) It seems a
priority_que ue from the STL would work fine. However at some point, I am
finished (even though the queue is not empty) and want to throw away the
rest of the elements. It seems, I have to do this using:

while (!Q.empty()) Q.pop();

Other classes offer a resize() or a clear() and my guess that this is
much more efficient than using a while-loop. However, these functions are
not available for a priority_queue. Any ideas? Thanks in advance for any
help.

Jan

Besides previously given answers... you could design things so that the
priority_queue goes out of scope when you're done with it or, if that's
not possible,
Not possible, it is part of another class, and I want to refill it starting
with an empty queue.,,
dynamically allocate it and attach it to a smart pointer
which you can manually delete as soon as convenient.
Ok, that would work of course, I was just hoping that there was something
more elegant and in "spirit" with the STL of just clearing a container. I
think Alf's solution is easiest and seems to reduce the time quite a bit.
(It only requires me changing one line of code ;-)))

Thanks,

Jan
(Implicit in this
is also the option to simply do nothing and wait for the queue to fall
out of scope, but I gather that this is not sufficient for you.)
Feb 6 '07 #7
Marcus Kwok schrieb:
Marcus Kwok <ri******@gehen nom.invalidwrot e:
>J.M. <jm************ ***@gmx.dewrote :
>>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.) It seems a
priority_queu e from the STL would work fine. However at some point, I am
finished (even though the queue is not empty) and want to throw away the
rest of the elements. It seems, I have to do this using:

while (!Q.empty()) Q.pop();

Other classes offer a resize() or a clear() and my guess that this is
much more efficient than using a while-loop. However, these functions
are not available for a priority_queue. Any ideas? Thanks in advance for
any help.

Alf P. Steinbach gave you a good answer. Another idea is to use a
different container and call std::make_heap( ) on it, but consistently
maintaining the heap property may be less efficient than just using the
priority queue.

After more investigation, <algorithmals o offers std::push_heap( ) and
std::pop_heap() in addition to std::make_heap( ), so maintaining the heap
property may not be as big of a hit as I thought. Of course, you will
need to measure for yourself if the performance is acceptable.
Well, I suppose all of that is feasible, but I was looking for a "quick fix"
and I think Alf's suggestion works best in that regard.

Thx

Jan
Feb 6 '07 #8
Alf P. Steinbach schrieb:
* J.M.:
>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.) It seems a
priority_que ue from the STL would work fine. However at some point, I am
finished (even though the queue is not empty) and want to throw away the
rest of the elements. It seems, I have to do this using:

while (!Q.empty()) Q.pop();

Other classes offer a resize() or a clear() and my guess that this is
much more efficient than using a while-loop. However, these functions are
not available for a priority_queue. Any ideas? Thanks in advance for any
help.

template< typename T >
void makeEmpty( std::priority_q ueue<T>& q )
{
std::priority_q ueue<Tempty;
std::swap( q, empty );
}
Seems to work quite well. Thx.

Jan
>
Of course, you need to measure measure measure.
Feb 6 '07 #9
J.M. schrieb:
Alf P. Steinbach schrieb:
>* J.M.:
>>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.) It seems a
priority_queu e from the STL would work fine. However at some point, I am
finished (even though the queue is not empty) and want to throw away the
rest of the elements. It seems, I have to do this using:

while (!Q.empty()) Q.pop();

Other classes offer a resize() or a clear() and my guess that this is
much more efficient than using a while-loop. However, these functions
are not available for a priority_queue. Any ideas? Thanks in advance for
any help.

template< typename T >
void makeEmpty( std::priority_q ueue<T>& q )
{
std::priority_q ueue<Tempty;
std::swap( q, empty );
}

Seems to work quite well. Thx.

Jan
Addendum:

It seems the following is even faster:

template< typename T >
void makeEmpty( std::priority_q ueue<T>& q )
{
std::priority_q ueue<Tempty;
q = empty;
}
>>
Of course, you need to measure measure measure.
Feb 6 '07 #10

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

Similar topics

4
6357
by: Tino | last post by:
Hopefully a simple question: What is the best/safest/fastest way to clear/make empty a std::priority_queue? Thanks, Ryan
7
3128
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<> a superior alternative. Why factors am I not considering here? Why in the general case would std::vector<> be best? Thanks, Dave
3
4478
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 doesn't seem to be anything like this for priority_queue. Also, I don't know anything about how priority_queue is implemented (specifically, MSVC++ 6.0), so maybe I don't have anything to worry about. Guidance, please? Regards, Ryan
3
4012
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 class first. To make it simple, here is the toy code. myClass.h ------------------------------------------ #ifndef MYCLASS_H #define MYCLASS_H
9
28796
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 find methods for these which suprises me as both should be easily solvable with access to the underlying vector. Did I just miss these methods? Can I somehow access the vector to get . begin() .end() and .find()?
6
11260
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 emptying it? Right now I'm using the following code: while (!pq.empty()) { cout << setw(3) << pq.top().first << ": " << pq.top().second <<
8
4162
by: thomas | last post by:
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:
24
2579
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 timestamp. // just a wrapper around priority_queue pq = new MyPriorityQueue(); // I generate 10 events w/ a random timestamp for (int i = 0; i < 10; i++) { MyEvent *event = new MyEvent();
5
3533
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 called Position with a comparator class called PositionCompare as follows: priority_queue<Position, vector<Position>, PositionCompare> _pQ; Now, I go along and keep inserting Position objects into _pQ. These Position objects are really...
0
10577
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10320
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10077
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9150
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7620
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6853
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4299
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2991
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.