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