我使用std::queue实现JobQueue类。(基本上这个类以FIFO方式处理每个作业)。 在一个场景中,我希望一次性清除队列(从队列中删除所有作业)。 我在std::queue类中没有看到任何可用的清除方法。

如何有效地实现JobQueue类的清除方法?

我有一个简单的解决方案弹出在一个循环,但我正在寻找更好的方法。

//Clears the job queue
void JobQueue ::clearJobs()
 {
  // I want to avoid pop in a loop
    while (!m_Queue.empty())
    {
        m_Queue.pop();
    }
}

当前回答

在c++ 11中,你可以通过这样做来清除队列:

std::queue<int> queue;
// ...
queue = {};

其他回答

Author of the topic asked how to clear the queue "efficiently", so I assume he wants better complexity than linear O(queue size). Methods served by David Rodriguez, anon have the same complexity: according to STL reference, operator = has complexity O(queue size). IMHO it's because each element of queue is reserved separately and it isn't allocated in one big memory block, like in vector. So to clear all memory, we have to delete every element separately. So the straightest way to clear std::queue is one line:

while(!Q.empty()) Q.pop();

在c++ 11中,你可以通过这样做来清除队列:

std::queue<int> queue;
// ...
queue = {};

清除标准容器的一个常用习语是交换容器的空版本:

void clear( std::queue<int> &q )
{
   std::queue<int> empty;
   std::swap( q, empty );
}

它也是清除某些容器内内存的唯一方法(std::vector)

我这样做(使用c++ 14):

std::queue<int> myqueue;
myqueue = decltype(myqueue){};

如果您有一个不平凡的队列类型,并且不想为其构建别名/typedef,则这种方法非常有用。不过,我总是确保就这种用法留下评论,向不知情的/维护程序员解释这并不疯狂,并代替了实际的clear()方法。

我宁愿不依赖swap()或将队列设置为新创建的队列对象,因为队列元素没有正确地销毁。调用pop()调用各自元素对象的析构函数。这在<int>队列中可能不是问题,但可能会对包含对象的队列产生副作用。

因此,如果您想防止可能的副作用,那么使用while(!queue.empty()) queue.pop();进行循环似乎是最有效的解决方案,至少对于包含对象的队列是如此。