Priority Queue - Remove

C

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.

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)
}
}
 
M

Michael Doubez

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.
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.

My compiler complains that member c is protected.

Here is the code I tried and failed with:
[snip]
 
J

James Kanze

queue and priority_queue are container adaptors whose sole
purpose is to forbid modification of the elements in order to
guarantee queue invariant.
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.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top