Skip to content

Closes #7134 : Added a new entry for swap() method under strings in C++. #7154

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

Merged
merged 8 commits into from
Jun 30, 2025
Merged
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
98 changes: 98 additions & 0 deletions content/cpp/concepts/strings/terms/swap/swap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
---
Title: '.swap()'
Description: 'Exchanges the contents of two strings.'
Subjects:
- 'Computer Science'
- 'Game Development'
Tags:
- 'Functions'
- 'Methods'
- 'Strings'
CatalogContent:
- 'learn-c-plus-plus'
- 'paths/computer-science'
---

In C++, the **`.swap()`** method for strings is used to exchange the contents of two strings efficiently. It is commonly used in scenarios like optimizing performance during reordering, implementing the copy-and-swap idiom in custom classes, or quickly clearing a string by swapping it with an empty one.

## Syntax

```pseudo
string1.swap(string2);
```

**Parameters:**

- `string2`: Another `std::string` object whose contents will be swapped with `string1`.

**Return value:**

This method performs the swap in-place and does not return a value.

After calling the `.swap()` method, the contents of `string1` and `string2` are exchanged: `string1` now holds the original contents of `string2`, and `string2` holds the original contents of `string1`.

## Example

In this example, the `.swap()` method is used to exchange the contents of two strings, `a` and `b`:

```py
#include <iostream>
#include <string>

int main() {
std::string a = "apple";
std::string b = "banana";

std::cout << "Before swap:\n";
std::cout << "a = " << a << "\n";
std::cout << "b = " << b << "\n";

// Swap the contents of a and b
a.swap(b);

std::cout << "\nAfter swap:\n";
std::cout << "a = " << a << "\n";
std::cout << "b = " << b << "\n";

return 0;
}
```

The output for this will be:

```shell
Before swap:
a = apple
b = banana

After swap:
a = banana
b = apple
```

## Codebyte Example

In this codebyte example, `.swap()` is called on the `place1` and `place2` strings:

```codebyte/cpp
#include <iostream>
#include <string>
int main() {
std::string place1 = "Los Angeles";
std::string place2 = "New York";
std::cout << "Before swap:\n";
std::cout << "Place 1 : " << place1 << "\n";
std::cout << "Place 2 : " << place2 << "\n";
place1.swap(place2);
std::cout << "After swap:\n";
std::cout << "Place 1 : " << place1 << "\n";
std::cout << "Place 2 : " << place2 << "\n";
return 0;
}
```