-
Notifications
You must be signed in to change notification settings - Fork 4.3k
[Term Entry] C++ Unordered-sets: swap() #8095
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sthuthi21
wants to merge
6
commits into
Codecademy:main
Choose a base branch
from
sthuthi21:swap
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e45c046
[Term Entry] C++ Unordered-sets: swap()
sthuthi21 fc73a88
Refine swap() method documentation in unordered_set
mamtawardhani db8780a
Fix formatting of swap method syntax examples
mamtawardhani 01fc123
Update content/cpp/concepts/unordered-set/terms/swap/swap.md
mamtawardhani 2662167
Clarify return value description in swap.md
mamtawardhani 8e4aa12
Merge branch 'main' into swap
sthuthi21 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 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_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 in the following syntax: | ||
|
|
||
| 1/. Member Function | ||
|
|
||
| ```pseudo | ||
| unordered_set_first.swap(unordered_set_second); | ||
| ``` | ||
|
|
||
| 2/. 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`). | ||
mamtawardhani marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Example | ||
|
|
||
| In this example, the contents of two `unordered_set` containers are exchanged using the member `swap()` function: | ||
|
|
||
| ```cpp | ||
| #include <iostream> | ||
| #include <unordered_set> | ||
| using namespace std; | ||
|
|
||
| int main() { | ||
| unordered_set<int> setA = {1, 2, 3}; | ||
| unordered_set<int> 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: | ||
|
|
||
| ```shell | ||
| Before swap: | ||
| setA contents: 3 2 1 | ||
| 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: | ||
|
|
||
| ```codebyte/cpp | ||
| #include <iostream> | ||
| #include <unordered_set> | ||
| using namespace std; | ||
|
|
||
| int main() { | ||
| unordered_set<string> fruits = {"apple", "banana"}; | ||
| unordered_set<string> 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; | ||
| } | ||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.