Skip to content

Commit e45c046

Browse files
committed
[Term Entry] C++ Unordered-sets: swap()
1 parent 4844888 commit e45c046

File tree

1 file changed

+132
-0
lines changed
  • content/cpp/concepts/unordered-set/terms/swap

1 file changed

+132
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
---
2+
Title: 'swap()'
3+
Description: 'Exchanges the contents of two unordered sets in constant time.'
4+
Subjects:
5+
- 'Code Foundations'
6+
- 'Computer Science'
7+
Tags:
8+
- 'Containers'
9+
- 'Sets'
10+
- 'STL'
11+
CatalogContent:
12+
- 'learn-c-plus-plus'
13+
- 'paths/computer-science'
14+
---
15+
16+
The **`swap()`** method exchanges the contents of one [`unordered_set`](https://www.codecademy.com/resources/docs/cpp/unordered-set) with another. This operation runs in constant time because it swaps internal structures rather than moving individual elements.
17+
18+
After the swap, both containers take ownership of each other's elements. Iterators and references remain valid but now refer to elements stored in the opposite container.
19+
20+
The two unordered sets need not have the same size but should have the same template parameters.
21+
22+
## Syntax
23+
24+
The `swap()` method can be used with the following syntax:
25+
26+
1. Member Function
27+
28+
```pseudo
29+
unordered_set_first.swap(unordered_set_second);
30+
```
31+
32+
1. Non-Member Function
33+
34+
```pseudo
35+
swap(unordered_set_first, unordered_set_second);
36+
```
37+
38+
**Parameters:**
39+
40+
- `unordered_set_second`: Another `unordered_set` with the same template parameters(element type, hash, predicate, allocator) as `unordered_set_first`.
41+
42+
**Return Value:**
43+
This method returns nothing (`void`).
44+
45+
## Example
46+
47+
This example demonstrates swapping contents between two `unordered_set` containers:
48+
49+
```cpp
50+
#include <iostream>
51+
#include <unordered_set>
52+
using namespace std;
53+
54+
int main() {
55+
unordered_set<int> setA = {1, 2, 3};
56+
unordered_set<int> setB = {10, 20};
57+
58+
cout << "Before swap:\n";
59+
cout << "setA contents: ";
60+
for (int x : setA) cout << x << " ";
61+
cout << "\n";
62+
63+
cout << "setB contents: ";
64+
for (int x : setB) cout << x << " ";
65+
cout << "\n";
66+
67+
setA.swap(setB);
68+
69+
cout << "\nAfter swap:\n";
70+
cout << "setA contents: ";
71+
for (int x : setA) cout << x << " ";
72+
cout << "\n";
73+
74+
cout << "setB contents: ";
75+
for (int x : setB) cout << x << " ";
76+
cout << "\n";
77+
78+
return 0;
79+
}
80+
81+
```
82+
83+
The expected output of this code is:
84+
85+
```
86+
Before swap:
87+
setA contents: 3 2 1
88+
setB contents: 20 10
89+
90+
After swap:
91+
setA contents: 20 10
92+
setB contents: 3 2 1
93+
94+
```
95+
96+
## Codebyte Example
97+
98+
In this example, the code swaps the contents of two `unordered_set` containers and prints their updated elements:
99+
100+
```codebyte/cpp
101+
#include <iostream>
102+
#include <unordered_set>
103+
using namespace std;
104+
105+
int main() {
106+
unordered_set<string> fruits = {"apple", "banana"};
107+
unordered_set<string> colors = {"red", "blue", "green"};
108+
109+
cout << "Before swap:\n";
110+
cout << "fruits: ";
111+
for (const auto& f : fruits) cout << f << " ";
112+
cout << "\n";
113+
114+
cout << "colors: ";
115+
for (const auto& c : colors) cout << c << " ";
116+
cout << "\n\n";
117+
118+
fruits.swap(colors);
119+
120+
cout << "After swap:\n";
121+
cout << "fruits: ";
122+
for (const auto& f : fruits) cout << f << " ";
123+
cout << "\n";
124+
125+
cout << "colors: ";
126+
for (const auto& c : colors) cout << c << " ";
127+
cout << "\n";
128+
129+
return 0;
130+
}
131+
132+
```

0 commit comments

Comments
 (0)