C++ Several methods of clearing the queue (queue) The queue in
C++ does not support the clear operation, but the deque of the deque does support the clear operation.
Method 1
Assign directly with an empty queue object
queue<int> q1;
// process
// ...
q1 = queue<int>();
Method 2:
Traverse out of the queue
while (!Q.empty()) Q.pop();
Method 3
Use swap, which is the most efficient, defines clear and maintains the standard of STL container.
void clear(queue<int>& q) {
queue<int> empty;
swap(empty, q);
}
————————————————
Copyright statement: This article is the original article of CSDN blogger "Pursuing Excellence 583", following the CC 4.0 BY-SA copyright agreement. Please attach the original source link and This statement.
Original link: https://blog.csdn.net/zhuiqiuzhuoyue583/article/details/82585383