From e45c046aeb9ce0ccb2523759b8b3e3160f2d337a Mon Sep 17 00:00:00 2001 From: sthuthi21 Date: Fri, 12 Dec 2025 03:26:02 +0530 Subject: [PATCH 1/5] [Term Entry] C++ Unordered-sets: swap() --- .../concepts/unordered-set/terms/swap/swap.md | 132 ++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 content/cpp/concepts/unordered-set/terms/swap/swap.md diff --git a/content/cpp/concepts/unordered-set/terms/swap/swap.md b/content/cpp/concepts/unordered-set/terms/swap/swap.md new file mode 100644 index 00000000000..a4557100bd5 --- /dev/null +++ b/content/cpp/concepts/unordered-set/terms/swap/swap.md @@ -0,0 +1,132 @@ +--- +Title: 'swap()' +Description: 'Exchanges the contents of two unordered sets in constant time.' +Subjects: + - 'Code Foundations' + - 'Computer Science' +Tags: + - 'Containers' + - 'Sets' + - 'STL' +CatalogContent: + - 'learn-c-plus-plus' + - 'paths/computer-science' +--- + +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. + +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. + +The two unordered sets need not have the same size but should have the same template parameters. + +## Syntax + +The `swap()` method can be used with the following syntax: + +1. Member Function + +```pseudo +unordered_set_first.swap(unordered_set_second); +``` + +1. Non-Member Function + +```pseudo +swap(unordered_set_first, unordered_set_second); +``` + +**Parameters:** + +- `unordered_set_second`: Another `unordered_set` with the same template parameters(element type, hash, predicate, allocator) as `unordered_set_first`. + +**Return Value:** +This method returns nothing (`void`). + +## Example + +This example demonstrates swapping contents between two `unordered_set` containers: + +```cpp +#include +#include +using namespace std; + +int main() { + unordered_set setA = {1, 2, 3}; + unordered_set setB = {10, 20}; + + cout << "Before swap:\n"; + cout << "setA contents: "; + for (int x : setA) cout << x << " "; + cout << "\n"; + + cout << "setB contents: "; + for (int x : setB) cout << x << " "; + cout << "\n"; + + setA.swap(setB); + + cout << "\nAfter swap:\n"; + cout << "setA contents: "; + for (int x : setA) cout << x << " "; + cout << "\n"; + + cout << "setB contents: "; + for (int x : setB) cout << x << " "; + cout << "\n"; + + return 0; +} + +``` + +The expected output of this code is: + +``` +Before swap: +setA contents: 3 2 1 +setB contents: 20 10 + +After swap: +setA contents: 20 10 +setB contents: 3 2 1 + +``` + +## Codebyte Example + +In this example, the code swaps the contents of two `unordered_set` containers and prints their updated elements: + +```codebyte/cpp +#include +#include +using namespace std; + +int main() { + unordered_set fruits = {"apple", "banana"}; + unordered_set colors = {"red", "blue", "green"}; + + cout << "Before swap:\n"; + cout << "fruits: "; + for (const auto& f : fruits) cout << f << " "; + cout << "\n"; + + cout << "colors: "; + for (const auto& c : colors) cout << c << " "; + cout << "\n\n"; + + fruits.swap(colors); + + cout << "After swap:\n"; + cout << "fruits: "; + for (const auto& f : fruits) cout << f << " "; + cout << "\n"; + + cout << "colors: "; + for (const auto& c : colors) cout << c << " "; + cout << "\n"; + + return 0; +} + +``` From fc73a88e5babac4102c47b345c511c15388b089b Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Sat, 13 Dec 2025 10:41:04 +0530 Subject: [PATCH 2/5] Refine swap() method documentation in unordered_set Updated wording for clarity and consistency in the swap method documentation. --- .../concepts/unordered-set/terms/swap/swap.md | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/content/cpp/concepts/unordered-set/terms/swap/swap.md b/content/cpp/concepts/unordered-set/terms/swap/swap.md index a4557100bd5..3a9771af132 100644 --- a/content/cpp/concepts/unordered-set/terms/swap/swap.md +++ b/content/cpp/concepts/unordered-set/terms/swap/swap.md @@ -13,23 +13,23 @@ CatalogContent: - 'paths/computer-science' --- -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. +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 by swapping internal structures rather than moving individual elements. 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. -The two unordered sets need not have the same size but should have the same template parameters. +The two `unordered_set` containers do not need to have the same size, but they must be of the same type. ## Syntax -The `swap()` method can be used with the following syntax: +The `swap()` method can be used in the following syntax: -1. Member Function +1/. Member Function ```pseudo unordered_set_first.swap(unordered_set_second); ``` -1. Non-Member Function +2/. Non-Member Function ```pseudo swap(unordered_set_first, unordered_set_second); @@ -39,12 +39,13 @@ swap(unordered_set_first, unordered_set_second); - `unordered_set_second`: Another `unordered_set` with the same template parameters(element type, hash, predicate, allocator) as `unordered_set_first`. -**Return Value:** +**Return value:** + This method returns nothing (`void`). ## Example -This example demonstrates swapping contents between two `unordered_set` containers: +In this example, the contents of two `unordered_set` containers are exchanged using the member `swap()` function: ```cpp #include @@ -77,12 +78,11 @@ int main() { return 0; } - ``` The expected output of this code is: -``` +```shell Before swap: setA contents: 3 2 1 setB contents: 20 10 @@ -90,9 +90,10 @@ setB contents: 20 10 After swap: setA contents: 20 10 setB contents: 3 2 1 - ``` +> **Note:** The output order may vary because `unordered_set` does not maintain a defined element order. + ## Codebyte Example In this example, the code swaps the contents of two `unordered_set` containers and prints their updated elements: @@ -128,5 +129,4 @@ int main() { return 0; } - ``` From db8780a2355274e47aa22abc30954de62f59434f Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Sat, 13 Dec 2025 10:41:42 +0530 Subject: [PATCH 3/5] Fix formatting of swap method syntax examples --- content/cpp/concepts/unordered-set/terms/swap/swap.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/cpp/concepts/unordered-set/terms/swap/swap.md b/content/cpp/concepts/unordered-set/terms/swap/swap.md index 3a9771af132..71e8debda18 100644 --- a/content/cpp/concepts/unordered-set/terms/swap/swap.md +++ b/content/cpp/concepts/unordered-set/terms/swap/swap.md @@ -23,13 +23,13 @@ The two `unordered_set` containers do not need to have the same size, but they m The `swap()` method can be used in the following syntax: -1/. Member Function +1\. Member Function ```pseudo unordered_set_first.swap(unordered_set_second); ``` -2/. Non-Member Function +2\. Non-Member Function ```pseudo swap(unordered_set_first, unordered_set_second); From 01fc123426177d61b852731c0faec86436a3face Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Sat, 13 Dec 2025 10:42:48 +0530 Subject: [PATCH 4/5] Update content/cpp/concepts/unordered-set/terms/swap/swap.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- content/cpp/concepts/unordered-set/terms/swap/swap.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/cpp/concepts/unordered-set/terms/swap/swap.md b/content/cpp/concepts/unordered-set/terms/swap/swap.md index 71e8debda18..12b3c6b8377 100644 --- a/content/cpp/concepts/unordered-set/terms/swap/swap.md +++ b/content/cpp/concepts/unordered-set/terms/swap/swap.md @@ -37,7 +37,7 @@ swap(unordered_set_first, unordered_set_second); **Parameters:** -- `unordered_set_second`: Another `unordered_set` with the same template parameters(element type, hash, predicate, allocator) as `unordered_set_first`. +- `unordered_set_second`: Another `unordered_set` with the same template parameters (element type, hash, predicate, allocator) as `unordered_set_first`. **Return value:** From 2662167e6b78568de1a4b000d48ed58f5317b7a0 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Sat, 13 Dec 2025 10:43:24 +0530 Subject: [PATCH 5/5] Clarify return value description in swap.md Updated the wording in the documentation for clarity. --- content/cpp/concepts/unordered-set/terms/swap/swap.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/cpp/concepts/unordered-set/terms/swap/swap.md b/content/cpp/concepts/unordered-set/terms/swap/swap.md index 12b3c6b8377..2de15eba483 100644 --- a/content/cpp/concepts/unordered-set/terms/swap/swap.md +++ b/content/cpp/concepts/unordered-set/terms/swap/swap.md @@ -41,7 +41,7 @@ swap(unordered_set_first, unordered_set_second); **Return value:** -This method returns nothing (`void`). +This function returns nothing (`void`). ## Example