Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't std::queue support a clear() function?

I have requirement: for a function, I get the input as a stream of numbers. I mean, the function keeps on getting called with single number in each call. I am using std::queue for storing the stream of numbers. I need to process a collected set of numbers only when some condition is satisfied. If the condition is not satisfied I need to put all the elements into the queue and then start storing new numbers in there. For emptying the queue, I couldn't find a clear() method. So I am looping like this:

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

I got an efficient algorithm for clearing a std::queue at

How do I clear the std::queue efficiently?

My question is: Why doesn't std::queue support a clear() function?

Since std::deque and std::vector both support a clear() method, what is the technical difficulty in supporting it for std::queue?

Or is my above use case very rare and hence not supported?

like image 567
bjskishore123 Avatar asked Oct 06 '10 16:10

bjskishore123


People also ask

How do you clear a std queue?

Apparently, there are two most obvious ways to clear std::queue : swapping with empty object and assignment to empty object. I would suggest using assignment because it simply faster, more readable, and unambiguous.

How do you clear the entire queue in C++?

deque::clear() The clear() function is used to remove all the elements of the deque container, thus making its size 0.

What does Clear () do in C++?

The C++ function std::list::clear() destroys the list by removing all elements from the list and sets size of list to zero.

Why is queue empty?

Queue is said to be empty when the value of front is at -1 or the value of front becomes greater than rear (front > rear).


4 Answers

Apart from what has been said already, you can clear a queue very easily:

queue<int> q;
...
q = queue<int>(); // Assign an empty queue

or in C++11

q = {};
like image 102
sellibitze Avatar answered Sep 29 '22 02:09

sellibitze


According to http://www.cplusplus.com/reference/stl/queue/,

queues are implemented as containers adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access it elements.

which means that the queue uses an already existing container, and is just really is an interface to this container as a FIFO queue.

This means queues are not meant to be cleared. If you need to clear a queue, this means you actually need to use an object that is not a queue, and therefore you should instead use the actual underlying container type, being a deque by default.

like image 29
SirDarius Avatar answered Sep 29 '22 02:09

SirDarius


queue is just an adapter for some underlying container, by default a deque, with restricted function (as you noted here). If you want the full blown function use the underlying deque instead of queue.

like image 40
Steve Townsend Avatar answered Sep 30 '22 02:09

Steve Townsend


Added this to my growing list of 'make STL readable' functions:

template <typename T> 
void Clear(std::queue<T>& Queue) 
{
    Queue = std::queue<T>(); // Assign to empty queue
}

It's just a wrapper around sellibitze's excellent answer, but means I don't have to also add a comment every time I use the technique.

like image 20
Andy Krouwel Avatar answered Sep 30 '22 02:09

Andy Krouwel