From 8766e22ed99635f022885b130a5cb7ab3f78218f Mon Sep 17 00:00:00 2001 From: alonsoserrudo Date: Mon, 30 Jun 2025 13:15:15 +0800 Subject: [PATCH 1/2] Create term --- content/cpp/concepts/sets/terms/swap/swap.md | 120 +++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 content/cpp/concepts/sets/terms/swap/swap.md diff --git a/content/cpp/concepts/sets/terms/swap/swap.md b/content/cpp/concepts/sets/terms/swap/swap.md new file mode 100644 index 00000000000..7b909502646 --- /dev/null +++ b/content/cpp/concepts/sets/terms/swap/swap.md @@ -0,0 +1,120 @@ +--- +Title: 'swap() (C++ set)' +Description: 'Exchanges the contents of two std::set containers in constant time.' +Subjects: + - 'C++' + - 'Sets' + - 'Standard Library' +Tags: + - 'cpp' + - 'set' + - 'swap' +CatalogContent: + - 'learn-cplusplus' + - 'paths/c-plus-plus' +--- + +**swap() exchanges the contents of two std::set containers (or two compatible ordered associative containers). Swapping is typically faster than moving elements one by one because it only exchanges internal pointers and metadata, keeping the elements themselves untouched.**. + +## Syntax + +**Non‑member overload (preferred)** + +using std::swap; +// (since C++11) +template< class Key, class Compare, class Alloc > +void swap( std::set< Key, Compare, Alloc >& lhs, + std::set< Key, Compare, Alloc >& rhs ); + +**Parameters** + +- lhs, rhs — sets whose contents will be exchanged. + +**Complexity** — Amortized O(1); pointers to elements remain valid. + +**Exceptions** — noexcept if the allocator's propagate‑on‑swap trait is true or the allocators compare equal. + +**Member function** + +set::swap( set& other ); // (since C++11) + +**Parameters** + +- other — the set whose contents will be exchanged with *this. + +**Complexity** — Constant. + +**Exceptions** — Same conditions as above. + +## Example + +#include +#include +#include + +int main() { + std::set odds {1, 3, 5, 7}; + std::set evens {2, 4, 6, 8}; + + std::cout << "Before swap → odds: "; + for (int n : odds) std::cout << n << ' '; + std::cout << "| evens: "; + for (int n : evens) std::cout << n << ' '; + std::cout << '\n'; + + std::swap(odds, evens); // non‑member swap + + std::cout << "After swap → odds: "; + for (int n : odds) std::cout << n << ' '; + std::cout << "| evens: "; + for (int n : evens) std::cout << n << ' '; + std::cout << '\n'; +} + +Output + +Before swap → odds: 1 3 5 7 | evens: 2 4 6 8 +After swap → odds: 2 4 6 8 | evens: 1 3 5 7 + +## Codebyte Example (if applicable) + +#include +#include +#include + +int main() { + std::set a {"apple", "banana"}; + std::set b {"carrot", "date", "eggplant"}; + + auto dump = [](const std::string& label, const std::set& s){ + std::cout << label << ": "; + for (const auto& v : s) std::cout << v << ' '; + std::cout << '\n'; + }; + + dump("a", a); + dump("b", b); + + a.swap(b); // member swap + + std::cout << "\nAfter swap\n"; + dump("a", a); + dump("b", b); +} + +We can currently support: + +- Python +- JavaScript +- Ruby +- C++ +- C# +- Go +- PHP + +See [content-standards.md](https://github.com/Codecademy/docs/blob/main/documentation/content-standards.md) for more details! + +```codebyte/js +# Example runnable code block. +console.log('Hello, World!'); +``` From d82bdd02bd122f262bee44d89a13a0127ffb4589 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Fri, 4 Jul 2025 09:01:25 +0530 Subject: [PATCH 2/2] content fixes --- content/cpp/concepts/sets/terms/swap/swap.md | 137 ++++++++----------- 1 file changed, 59 insertions(+), 78 deletions(-) diff --git a/content/cpp/concepts/sets/terms/swap/swap.md b/content/cpp/concepts/sets/terms/swap/swap.md index 7b909502646..ccf17b5ee02 100644 --- a/content/cpp/concepts/sets/terms/swap/swap.md +++ b/content/cpp/concepts/sets/terms/swap/swap.md @@ -1,120 +1,101 @@ --- -Title: 'swap() (C++ set)' -Description: 'Exchanges the contents of two std::set containers in constant time.' +Title: 'swap()' +Description: 'Exchanges the contents of two sets.' Subjects: - - 'C++' - - 'Sets' - - 'Standard Library' + - 'Code Foundations' + - 'Computer Science' Tags: - - 'cpp' - - 'set' - - 'swap' + - 'Containers' + - 'Sets' CatalogContent: - - 'learn-cplusplus' - - 'paths/c-plus-plus' + - 'learn-c-plus-plus' + - 'paths/computer-science' --- -**swap() exchanges the contents of two std::set containers (or two compatible ordered associative containers). Swapping is typically faster than moving elements one by one because it only exchanges internal pointers and metadata, keeping the elements themselves untouched.**. +**`swap()`** exchanges the contents of two `std::set` containers (or two compatible ordered associative containers). This operation is typically faster than copying or moving elements individually, as it swaps internal pointers and metadata without modifying or relocating the actual elements. ## Syntax -**Non‑member overload (preferred)** - -using std::swap; -// (since C++11) -template< class Key, class Compare, class Alloc > -void swap( std::set< Key, Compare, Alloc >& lhs, - std::set< Key, Compare, Alloc >& rhs ); - -**Parameters** - -- lhs, rhs — sets whose contents will be exchanged. - -**Complexity** — Amortized O(1); pointers to elements remain valid. - -**Exceptions** — noexcept if the allocator's propagate‑on‑swap trait is true or the allocators compare equal. +```pseudo +set1.swap(set2); +``` -**Member function** +Or using the non-member function: -set::swap( set& other ); // (since C++11) +```pseudo +std::swap(set1, set2); +``` **Parameters** -- other — the set whose contents will be exchanged with *this. +- `set1`, `set2`: Two `std::set` containers of the same type (including the same key type, comparator, and allocator). -**Complexity** — Constant. +**Return value:** -**Exceptions** — Same conditions as above. +- The function does not return anything. It simply swaps the contents. ## Example +In this example, `std::swap()` is used to exchange the contents of two integer sets named `odds` and `evens`: + +```cpp #include #include #include int main() { - std::set odds {1, 3, 5, 7}; - std::set evens {2, 4, 6, 8}; - - std::cout << "Before swap → odds: "; - for (int n : odds) std::cout << n << ' '; - std::cout << "| evens: "; - for (int n : evens) std::cout << n << ' '; - std::cout << '\n'; - - std::swap(odds, evens); // non‑member swap - - std::cout << "After swap → odds: "; - for (int n : odds) std::cout << n << ' '; - std::cout << "| evens: "; - for (int n : evens) std::cout << n << ' '; - std::cout << '\n'; + std::set odds {1, 3, 5, 7}; + std::set evens {2, 4, 6, 8}; + + std::cout << "Before swap → odds: "; + for (int n : odds) std::cout << n << ' '; + std::cout << "| evens: "; + for (int n : evens) std::cout << n << ' '; + std::cout << '\n'; + + std::swap(odds, evens); // non‑member swap + + std::cout << "After swap → odds: "; + for (int n : odds) std::cout << n << ' '; + std::cout << "| evens: "; + for (int n : evens) std::cout << n << ' '; + std::cout << '\n'; } +``` -Output +The output of this code is: +```shell Before swap → odds: 1 3 5 7 | evens: 2 4 6 8 After swap → odds: 2 4 6 8 | evens: 1 3 5 7 +``` -## Codebyte Example (if applicable) +## Codebyte Example +In this example, the member function `swap()` is used to swap the contents of two string sets, `a` and `b`: + +```codebyte/cpp #include #include #include int main() { - std::set a {"apple", "banana"}; - std::set b {"carrot", "date", "eggplant"}; + std::set a {"apple", "banana"}; + std::set b {"carrot", "date", "eggplant"}; - auto dump = [](const std::string& label, const std::set& s){ - std::cout << label << ": "; - for (const auto& v : s) std::cout << v << ' '; - std::cout << '\n'; - }; + auto dump = [](const std::string& label, const std::set& s){ + std::cout << label << ": "; + for (const auto& v : s) std::cout << v << ' '; + std::cout << '\n'; + }; - dump("a", a); - dump("b", b); + dump("a", a); + dump("b", b); - a.swap(b); // member swap + a.swap(b); // member swap - std::cout << "\nAfter swap\n"; - dump("a", a); - dump("b", b); + std::cout << "\nAfter swap\n"; + dump("a", a); + dump("b", b); } - -We can currently support: - -- Python -- JavaScript -- Ruby -- C++ -- C# -- Go -- PHP - -See [content-standards.md](https://github.com/Codecademy/docs/blob/main/documentation/content-standards.md) for more details! - -```codebyte/js -# Example runnable code block. -console.log('Hello, World!'); ```