Skip to content

[Term entry] C++Sets Swap() #7185

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
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions content/cpp/concepts/sets/terms/swap/swap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
---
Title: 'swap()'
Description: 'Exchanges the contents of two sets.'
Subjects:
- 'Code Foundations'
- 'Computer Science'
Tags:
- 'Containers'
- 'Sets'
CatalogContent:
- 'learn-c-plus-plus'
- 'paths/computer-science'
---

**`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

```pseudo
set1.swap(set2);
```

Or using the non-member function:

```pseudo
std::swap(set1, set2);
```

**Parameters**

- `set1`, `set2`: Two `std::set` containers of the same type (including the same key type, comparator, and allocator).

**Return value:**

- 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 <iostream>
#include <set>
#include <algorithm>

int main() {
std::set<int> odds {1, 3, 5, 7};
std::set<int> 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';
}
```

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

In this example, the member function `swap()` is used to swap the contents of two string sets, `a` and `b`:

```codebyte/cpp
#include <iostream>
#include <set>
#include <algorithm>

int main() {
std::set<std::string> a {"apple", "banana"};
std::set<std::string> b {"carrot", "date", "eggplant"};

auto dump = [](const std::string& label, const std::set<std::string>& 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);
}
```
Loading