STL: find() and iteration on priority_queue

H

Henning Hasemann

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()?

TIA
Henning
 
R

Rolf Magnus

Henning said:
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.

Well, they are not operations that are supposed to be done with a queue.
There is not much point in making a queue that offers random access. It
just wouldn't qualify as queue anymore.
Did I just miss these methods? Can I somehow access the vector to get .
begin() .end() and .find()?

If you want the functionality of a vector, just use a vector.
 
H

Henning Hasemann

Rolf said:
Well, they are not operations that are supposed to be done with a queue.
There is not much point in making a queue that offers random access. It
just wouldn't qualify as queue anymore.

I dont want random access. I just want a way to read all elements
without having to deconstruct the queue. (read-only acces is enough for me)
If you want the functionality of a vector, just use a vector.

That comment was rather unhelpful.
I want the functionality of a priority_queue because I need fast access
to the "highest" element.
I also need to be able to find out if a certain element is contained.
Iterating isnt even necessary if I'd had only a contains()-method or
something.

To give you the whole picture I'm talking about a container for the
open-"list" used in the dijkstra algorithm and the 3 operations done
with it are inerting an element, getting and removing the element with
the highest value and testing if a certain element is contained.

Unfortunately the code is really time-critical and using a list and
insertion sort is simply to slow.

Any suggestions?
 
J

joosteto

Did I just miss these methods? Can I somehow access the vector to get .
That comment was rather unhelpful.
I want the functionality of a priority_queue because I need fast access
to the "highest" element.
I also need to be able to find out if a certain element is contained.
Iterating isnt even necessary if I'd had only a contains()-method or
something.

In that case, what's wrong with map or multimap? Seem to have all your
requirements?
 
K

Kai-Uwe Bux

Henning said:
I dont want random access. I just want a way to read all elements
without having to deconstruct the queue. (read-only acces is enough for
me)


That comment was rather unhelpful.
I want the functionality of a priority_queue because I need fast access
to the "highest" element.
I also need to be able to find out if a certain element is contained.
Iterating isnt even necessary if I'd had only a contains()-method or
something.

To give you the whole picture I'm talking about a container for the
open-"list" used in the dijkstra algorithm and the 3 operations done
with it are inerting an element, getting and removing the element with
the highest value and testing if a certain element is contained.

Unfortunately the code is really time-critical and using a list and
insertion sort is simply to slow.

Any suggestions?

A) You can use a BAD HACK (tm):


#include <queue>
#include <iostream>
#include <algorithm>
#include <iterator>


typedef std::priority_queue<int> int_priority_queue;

template < typename T >
T const * q_begin ( std::priority_queue<T> const & q ) {
return ( &( q.top() ) );
}

template < typename T >
T const * q_end ( std::priority_queue<T> const & q ) {
return ( &( q.top() ) + q.size() );
}



int main ( void ) {
int_priority_queue q;
q.push( 1 );
q.push( 2 );
q.push( 3 );
std::copy( q_begin( q ), q_end( q ),
std::eek:stream_iterator<int>( std::cout, " " ) );
std::cout << '\n';
}


Note that this is based on two assumptions:

a) The underlying sequence container is contiguous. This is guaranteed by
the standard for std::vector.

b) The priority_queue is implemented so that the top() element comes first
in the sequence. This is the way, heaps are organized. However, the
standard does not guarantee that priority_queue<T> works this way. YMMV.



B) You can use a vector directly: <algorithm> provides push_heap() and
pop_heap() for any range.


C) For further speed-up, you may consider making use of the heap-structure
when deciding whether a certain element is contained in the queue: it
should take log( size() ) comparisons.


Best

Kai-Uwe Bux
 
K

Kristo

Henning said:
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()?

You didn't miss them because they're not provided *on purpose*. See
http://www.sgi.com/tech/stl/priority_queue.html (scroll down to note 2)
for reasons why and what you can do about it.

Kristo
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Henning said:
To give you the whole picture I'm talking about a container for the
open-"list" used in the dijkstra algorithm and the 3 operations done
with it are inerting an element, getting and removing the element with
the highest value and testing if a certain element is contained.
Unfortunately the code is really time-critical and using a list and
insertion sort is simply to slow.
Any suggestions?

Other than write some code that does what you want?
 
R

red floyd

Henning said:
I dont want random access. I just want a way to read all elements
without having to deconstruct the queue. (read-only acces is enough for me)


That comment was rather unhelpful.
I want the functionality of a priority_queue because I need fast access
to the "highest" element.
I also need to be able to find out if a certain element is contained.
Iterating isnt even necessary if I'd had only a contains()-method or
something.

To give you the whole picture I'm talking about a container for the
open-"list" used in the dijkstra algorithm and the 3 operations done
with it are inerting an element, getting and removing the element with
the highest value and testing if a certain element is contained.

Unfortunately the code is really time-critical and using a list and
insertion sort is simply to slow.

Use private inheritance to inherit from priority_queue(). The
underlying container is a protected member. Make sure you make the
inherited functions visible with using declarations.
 
K

Kristo

red said:
Use private inheritance to inherit from priority_queue(). The
underlying container is a protected member. Make sure you make the
inherited functions visible with using declarations.

I don't have a copy of the standard here, but I suspect that's an
implementation detail. I wouldn't rely on the underlying container
being protected if you're trying to write code that is portable across
compilers.
 
R

red floyd

Kristo said:
I don't have a copy of the standard here, but I suspect that's an
implementation detail. I wouldn't rely on the underlying container
being protected if you're trying to write code that is portable across
compilers.

Actually, I got that info from Josuttis. Also, I just checked -- the
Standard 23.2.3.2/1 shows the underlying container and comparison
functions as protected members. So it's not an implementation detail.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top