Great answers above...
If I were writing this queue it might look something like this...
#pragma once
#include <map>
#include <deque>
template<class T>
class CPriorityQueueT
{
protected:
std::map<int, std::deque<T>> PriorityMap;
public:
bool PushMessage(const T & Message, int Priority = 5);
public:
bool PopMessage(T & Message, int * pPriority = NULL);
};
template<class T>
bool CPriorityQueueT<T>::PushMessage(const T & Message, int Priority)
{
std::map<int, std::deque<T>>::iterator Iter = PriorityMap.find(Priority);
if (Iter == PriorityMap.end())
{
PriorityMap.insert(std::pair<int, std::deque<T>>(Priority, std::deque<T>()));
Iter = PriorityMap.find(Priority);
}
std::deque<T> & Deque = (*Iter).second;
Deque.push_back(Message);
return true;
}
template<class T>
bool CPriorityQueueT<T>::PopMessage(T & Message, int * pPriority)
{
std::map<int, std::deque<T>>::iterator Iter = PriorityMap.begin();
std::map<int, std::deque<T>>::iterator End = PriorityMap.end();
for (; Iter != End; Iter++)
{
if ((*Iter).second.size())
break;
}
if (Iter == PriorityMap.end())
{
return false;
}
if (pPriority)
*pPriority = (*Iter).first;
std::deque<T> & Deque = (*Iter).second;
Message = Deque.front();
Deque.pop_front();
return true;
}