You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* add swap file to cpp terms
* add updated swap file to cpp terms
* correct terms and cleaned example
* add example section with output
* newline
* minor fixes
* Minor changes
---------
In C++, the **`.swap()`** function swaps the values of two queues. The queues must have the same [data type](https://www.codecademy.com/resources/docs/cpp/data-types), although size can differ.
17
+
18
+
## Syntax
19
+
20
+
The `.swap()` method can be used with the following syntax:
21
+
22
+
```pseudo
23
+
queue1.swap(queue2);
24
+
```
25
+
26
+
Or alternatively:
27
+
28
+
```pseudo
29
+
swap(queue1, queue2);
30
+
```
31
+
32
+
**Parameters:**
33
+
34
+
-`queue2`: The other queue whose contents are to be swapped.
35
+
36
+
**Return value:**
37
+
38
+
`.swap()` does not return a value.
39
+
40
+
## Example
41
+
42
+
This example demonstrates the usage of the `.swap()` method with string queues:
43
+
44
+
```cpp
45
+
#include<iostream>
46
+
#include<queue>
47
+
usingnamespacestd;
48
+
49
+
intmain() {
50
+
// Declare queues
51
+
queue<string> hello;
52
+
queue<string> world;
53
+
54
+
// Populate queues
55
+
hello.push("hello");
56
+
world.push("world");
57
+
58
+
// Swap the values of the queues
59
+
hello.swap(world);
60
+
61
+
// Print queue values
62
+
cout << hello.front() << " " << world.front();
63
+
}
64
+
```
65
+
66
+
The expected output would be:
67
+
68
+
```
69
+
world hello
70
+
```
71
+
72
+
## Codebyte Example
73
+
74
+
The following codebyte example below uses `.swap()` on two integer queues:
0 commit comments