Discussion:
Priority Queue - Remove
(too old to reply)
Christopher
2009-05-25 09:46:18 UTC
Permalink
First time using the std::priority_queue.

I am trying to come up with a means to remove an object from the
queue. The only available method I see is pop, which will not suffice.
I want to search through it and find the object to remove.

I imagine I need access to the underlying container. How can I get it?
My compiler complains that member c is protected.

Here is the code I tried and failed with:


#ifndef RENDERQUEUE_H
#define RENDERQUEUE_H

// EngineX Includes
#include "IRenderable.h"

// Standard Includes
#include <queue>

//------------------------------------------------------------------------------
class RenderQueue
{
public:

RenderQueue();
~RenderQueue();
void Insert(IRenderable * object);
void Remove(IRenderable * object);

protected:

struct Compare
{
bool operator () (const IRenderable * lhs, const IRenderable *
rhs);
};

typedef std::priority_queue<IRenderable *,
std::vector<IRenderable *>,
Compare > Queue;
Queue m_queue;

private:

};

#endif // RENDERQUEUE_H



//------------------------------------------------------------------------------
void RenderQueue::Remove(IRenderable * object)
{
Queue::container_type::iterator it = m_queue.c.end();

if( !m_queue.empty() )
{
it = std::find(m_queue.c.begin(), m_queue.c.end(), object);
}

if( it != m_queue.c.end() )
{
m_queue.c.erase(it)
}
}
Michael Doubez
2009-05-25 10:36:33 UTC
Permalink
Post by Christopher
First time using the std::priority_queue.
I am trying to come up with a means to remove an object from the
queue. The only available method I see is pop, which will not suffice.
I want to search through it and find the object to remove.
queue and priority_queue are container adaptors whose sole purpose is
to forbid modification of the elements in order to guarantee queue
invariant.
Post by Christopher
I imagine I need access to the underlying container. How can I get it?
As such, you can neither access the underlying container nor iterate
through its elements.

I don't know why an erase() member function was not provided since it
doesn't break the invariant but here we are.

Instead, you can use a classical container such as deque or vector and
sort it.
Post by Christopher
My compiler complains that member c is protected.
[snip]
James Kanze
2009-05-25 15:13:35 UTC
Permalink
Post by Michael Doubez
Post by Christopher
First time using the std::priority_queue.
I am trying to come up with a means to remove an object from
the queue. The only available method I see is pop, which
will not suffice. I want to search through it and find the
object to remove.
queue and priority_queue are container adaptors whose sole
purpose is to forbid modification of the elements in order to
guarantee queue invariant.
Post by Christopher
I imagine I need access to the underlying container. How can
I get it?
As such, you can neither access the underlying container nor
iterate through its elements.
I don't know why an erase() member function was not provided
since it doesn't break the invariant but here we are.
Instead, you can use a classical container such as deque or
vector and sort it.
You can also derive from the class, and add the desired member
functions in the derived class.

--
James Kanze (GABI Software) email:***@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Continue reading on narkive:
Loading...