[英]Removing the first element from a queue?
I have a queue. 我有一个队列。 If it exceeds X size, when I push an element I want to remove the first element of the queue.
如果它超过X大小,则当我推送一个元素时,我想删除队列的第一个元素。 (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 谢谢
You can use a std::deque
instead of a std::queue
, which supports push_front
, push_back
, pop_front
, and pop_back
. 您可以使用
std::deque
代替std::queue
,后者支持push_front
, push_back
, pop_front
和pop_back
。 This also allows for random access throughout, but you can just ignore that and treat the deque
like a double-ended queue. 这也使得随机访问贯穿始终,但你可以忽略这一点,对待
deque
类似双端队列。 (In fact, deque
is short for double-ended queue). (实际上,
deque
是双端队列的缩写)。
Hope this helps! 希望这可以帮助!
If you want to remove an element from the opposite end from what pop
does, just skip pushing it in the first place: 如果要从
pop
的另一端删除元素,只需跳过首先将其推入的步骤:
if(m_tableQ.size() < m_maxTableMessages) {
m_tableQ.push(std::make_pair(playerName,message));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.