C++ set swap() Function
Example
Swap the contents of two sets:
set<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};
set<string> fruits = {"Apple", "Banana", "Cherry"};
cars.swap(fruits);
for (string item : cars) {
cout << item << "\n";
}
Definition and Usage
The swap() function swaps the contents of two sets. After the swap, each set has the elements that the other set had before.
Both sets must have the same type.
The swap function is defined in the <set> header file.
Syntax
set.swap(otherSet)
Parameter Values
| Parameter | Description |
|---|---|
| otherSet | Required. Another set with the same type. |
Technical Details
| Returns: | Nothing (void). |
|---|
Related Pages
Read more about sets in our Sets Tutorial.