From 75d78a2614f734c7c71a7f62e43a9a28e8063ac3 Mon Sep 17 00:00:00 2001 From: GR333N893 Date: Wed, 1 Oct 2025 12:09:42 -0600 Subject: [PATCH 1/5] add swap file to cpp terms --- .../cpp/concepts/queues/terms/swap/swap.md | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 content/cpp/concepts/queues/terms/swap/swap.md diff --git a/content/cpp/concepts/queues/terms/swap/swap.md b/content/cpp/concepts/queues/terms/swap/swap.md new file mode 100644 index 00000000000..ea403da6522 --- /dev/null +++ b/content/cpp/concepts/queues/terms/swap/swap.md @@ -0,0 +1,45 @@ +--- +Title: 'swap()' +Description: 'Swaps the values of two variables.' +Subjects: + - 'Computer Science' + - 'Game Development' +Tags: + - 'Objects' + - 'OOP' + - 'Classes' +CatalogContent: + - 'learn-c-plus-plus' + - 'paths/computer-science' +--- + +The **`swap()`** function swaps the values of two variables. The variables must have the same [data type](https://www.codecademy.com/resources/docs/cpp/data-types). + +## Syntax + +The `swap()` function takes two parameters using the following syntax: + +```pseudo +swap(a, b); +``` + +## Codebyte Example + +The following codebyte exapmle below uses `swap()` on two int variables: + +```codebyte/cpp +#include +using namespace std; + +int main() { + // Declare variables + int a = 1, b = 2; + + // Swap the values of a and b + swap(a, b); + + // Print variable values + cout << "a: " << a << endl; + cout << "b: " << b; +} +``` \ No newline at end of file From 1a1fe9b9d969f4a56402d7ef8593037c04786e98 Mon Sep 17 00:00:00 2001 From: GR333N893 Date: Thu, 2 Oct 2025 18:01:34 -0600 Subject: [PATCH 2/5] add updated swap file to cpp terms --- .../cpp/concepts/queues/terms/swap/swap.md | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/content/cpp/concepts/queues/terms/swap/swap.md b/content/cpp/concepts/queues/terms/swap/swap.md index ea403da6522..1ed1bd3795d 100644 --- a/content/cpp/concepts/queues/terms/swap/swap.md +++ b/content/cpp/concepts/queues/terms/swap/swap.md @@ -1,6 +1,6 @@ --- -Title: 'swap()' -Description: 'Swaps the values of two variables.' +Title: '.swap()' +Description: 'Swaps the values of two queues.' Subjects: - 'Computer Science' - 'Game Development' @@ -13,33 +13,41 @@ CatalogContent: - 'paths/computer-science' --- -The **`swap()`** function swaps the values of two variables. The variables must have the same [data type](https://www.codecademy.com/resources/docs/cpp/data-types). +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. ## Syntax -The `swap()` function takes two parameters using the following syntax: +The `.swap()` method can be used with the following syntax: ```pseudo -swap(a, b); +a.swap(b); // or swap(a, b); ``` ## Codebyte Example -The following codebyte exapmle below uses `swap()` on two int variables: +The following codebyte exapmle below uses `.swap()` on two int queues: ```codebyte/cpp #include +#include using namespace std; int main() { - // Declare variables - int a = 1, b = 2; + // Declare queues + queue a; + queue b; + + // Populate queues + a.push(1); + a.push(2); + b.push(3); + b.push(4); // Swap the values of a and b - swap(a, b); + a.swap(b); // or swap(a, b); // Print variable values - cout << "a: " << a << endl; - cout << "b: " << b; + cout << "a: " << a.front() << ", " << a.back() << endl; + cout << "b: " << b.front() << ", " << b.back(); } ``` \ No newline at end of file From 38809a69a8aaa6c0d90d46a19817598498ac0c30 Mon Sep 17 00:00:00 2001 From: GR333N893 Date: Thu, 2 Oct 2025 18:13:34 -0600 Subject: [PATCH 3/5] correct terms and cleaned example --- content/cpp/concepts/queues/terms/swap/swap.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/cpp/concepts/queues/terms/swap/swap.md b/content/cpp/concepts/queues/terms/swap/swap.md index 1ed1bd3795d..79db25b513a 100644 --- a/content/cpp/concepts/queues/terms/swap/swap.md +++ b/content/cpp/concepts/queues/terms/swap/swap.md @@ -44,9 +44,9 @@ int main() { b.push(4); // Swap the values of a and b - a.swap(b); // or swap(a, b); + a.swap(b); - // Print variable values + // Print queue values cout << "a: " << a.front() << ", " << a.back() << endl; cout << "b: " << b.front() << ", " << b.back(); } From 3d5421a50ffad5071aa69a51cc9dbee508f9751f Mon Sep 17 00:00:00 2001 From: frowningfrog Date: Fri, 3 Oct 2025 10:47:54 -0600 Subject: [PATCH 4/5] add example section with output --- .../cpp/concepts/queues/terms/swap/swap.md | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/content/cpp/concepts/queues/terms/swap/swap.md b/content/cpp/concepts/queues/terms/swap/swap.md index 79db25b513a..17ae20c662b 100644 --- a/content/cpp/concepts/queues/terms/swap/swap.md +++ b/content/cpp/concepts/queues/terms/swap/swap.md @@ -20,7 +20,39 @@ The **`.swap()`** function swaps the values of two queues. The queues must have The `.swap()` method can be used with the following syntax: ```pseudo -a.swap(b); // or swap(a, b); +queue1.swap(queue2); // or swap(queue1, queue2); +``` + +## Example + +This example demonstrates the usage of the swap method with string queues: + +```cpp +#include +#include +using namespace std; + +int main() { + // Declare queues + queue hello; + queue world; + + // Populate queues + hello.push("hello"); + world.push("world"); + + // Swap the values of the queues + hello.swap(world); + + // Print queue values + cout << hello.front() << " " << world.front(); +} +``` + +The expected output would be: + +``` +world hello ``` ## Codebyte Example From 267d68e8f2372f0bfc0a9cd18d01f0701d7980b8 Mon Sep 17 00:00:00 2001 From: frowningfrog Date: Fri, 3 Oct 2025 11:39:08 -0600 Subject: [PATCH 5/5] newline --- content/cpp/concepts/queues/terms/swap/swap.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/cpp/concepts/queues/terms/swap/swap.md b/content/cpp/concepts/queues/terms/swap/swap.md index 17ae20c662b..7db352122ec 100644 --- a/content/cpp/concepts/queues/terms/swap/swap.md +++ b/content/cpp/concepts/queues/terms/swap/swap.md @@ -82,4 +82,4 @@ int main() { cout << "a: " << a.front() << ", " << a.back() << endl; cout << "b: " << b.front() << ", " << b.back(); } -``` \ No newline at end of file +```