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