Reputation: 54103
I have a queue. If it exceeds X size, when I push an element I want to remove the first element of the queue. (the last element that would get popped and the first element pushed in)
void ClientPlayerManager::queueTableMessage( const std::string& playerName, const std::string& message )
{
m_tableQ.push(std::make_pair(playerName,message));
if(m_tableQ.size() > m_maxTableMessages)
{
//m_tableQ.pop_back(); does not exist
}
}
Is there a way to do this with a std queue?
Thanks
Upvotes: 2
Views: 8853
Reputation: 283614
If you want to remove an element from the opposite end from what pop
does, just skip pushing it in the first place:
if(m_tableQ.size() < m_maxTableMessages) {
m_tableQ.push(std::make_pair(playerName,message));
}
Upvotes: 0
Reputation: 372714
You can use a std::deque
instead of a std::queue
, which supports push_front
, push_back
, pop_front
, and pop_back
. This also allows for random access throughout, but you can just ignore that and treat the deque
like a double-ended queue. (In fact, deque
is short for double-ended queue).
Hope this helps!
Upvotes: 6