C++ queue swap() Function
Example
Swap the contents of two queues:
queue<string> cars;
queue<string> fruits;
cars.push("Volvo");
fruits.push("Apple");
cars.swap(fruits);
cout << cars.front() << "\n";
cout << fruits.front();
Definition and Usage
The swap() function swaps the contents of two queues. After the swap, each queue has the elements that the other queue had before.
Both queues must store the same type of data, but they can have a different number of elements.
The swap function is defined in the <queue> header file.
Syntax
queue.swap(queue<type> other);
<type> refers to the type of the data that the queue contains.
Parameter Values
| Parameter | Description |
|---|---|
| other | Required. Another queue with the same type of data. |
Technical Details
| Returns: | Nothing (void). |
|---|
Related Pages
Read more about queues in our Queues Tutorial.