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:

riority_queue<int> int_priority_queue;
template < typename T >
T const * q_begin ( std:

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

riority_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:

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