# std::basic_string<CharT,Traits,Allocator>::swap

> C++ Atlas: `cppatlas.com/w/cpp/string/basic_string/swap` | Source: https://en.cppreference.com/w/cpp/string/basic_string/swap | Domain: Strings | Standards: C++11, C++17, C++20

Exchanges the contents of the string with those of other. All iterators and references may be invalidated.

## Official Specification Summary (cppreference)

```cpp
void swap( basic_string& other ); // (until C++17)
void swap( basic_string& other ) noexcept(/* see below */); // (since C++17) (constexpr since C++20)
```

Exchanges the contents of the string with those of `other`. All iterators and references may be invalidated.

If `std::allocator_traits<Allocator>:: propagate_on_container_swap::value &&get_allocator() == s.get_allocator()` is `false`, the behavior is undefined. (since C++11)

### Parameters

- `other` - string to exchange the contents with

### Complexity

Constant.

### Exceptions

No exception is thrown. (until C++11)

(since C++11):

No exception is thrown, unless the behavior is undefined.

If an exception is thrown for any reason, this function has no effect (strong exception safety guarantee).

(since C++17):

`noexcept` specification:

`noexcept(std::allocator_traits<Allocator>::propagate_on_container_swap::value || std::allocator_traits<Allocator>::is_always_equal::value)`

### Example

```cpp
#include <iostream>
#include <string>

int main()
{
    std::string a = "AAA";
    std::string b = "BBBB";

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

    a.swap(b);

    std::cout << "After swap:\n"
                 "a = " << a << "\n"
                 "b = " << b << '\n';
}
```

Output:

```text
Before swap:
a = AAA
b = BBBB

After swap:
a = BBBB
b = AAA
```

### Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

| DR | Applied to | Behavior as published | Correct behavior |
| --- | --- | --- | --- |
| LWG 403 | C++98 | `swap()` might throw an exception | no exception is thrown |
| LWG 535 | C++98 | swapping strings did not preserve the character orders | orders are also preserved |
| LWG 2151 (P1148R0) | C++11 | no exception was thrown in the case of unequal non-propagating allocators | the behavior is undefined in this case |

### See also

- [`swap`](https://en.cppreference.com/w/cpp/algorithm/swap): swaps the values of two objects (function template)
- [`swap_ranges`](https://en.cppreference.com/w/cpp/algorithm/swap_ranges): swaps two ranges of elements (function template)
- [`swap`](https://en.cppreference.com/w/cpp/string/basic_string_view/swap): swaps the contents (public member function of `std::basic_string_view<CharT,Traits>`)

---
Attribution: content from https://en.cppreference.com/w/cpp/string/basic_string/swap (cppreference.com), licensed under CC-BY-SA 3.0 and GFDL. Modified: converted to markdown, study notes added. Offline snapshot of 9 February 2025.