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

> C++ Atlas: `cppatlas.com/w/cpp/io/basic_stringbuf/swap` | Source: https://en.cppreference.com/w/cpp/io/basic_stringbuf/swap | Domain: Input/output | Standards: C++11, C++20

Swaps the state and the contents of *this and rhs.

## Official Specification Summary (cppreference)

```cpp
void swap( basic_stringbuf& rhs ); // (since C++11) (until C++20)
void swap( basic_stringbuf& rhs ) noexcept(/* see below */); // (since C++20)
```

Swaps the state and the contents of `*this` and `rhs`.

The behavior is undefined if `Allocator` does not propagate on swap and the allocators of `*this` and `other` are unequal. (since C++11)

### Parameters

- `rhs` - another `basic_stringbuf`

### Return value

(none)

### Exceptions

May throw implementation-defined exceptions. (since C++11) (until C++20)

(since C++20):

`noexcept` specification:

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

### Notes

This function is called automatically when swapping `std::stringstream` objects. It is rarely necessary to call it directly.

### Example

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

int main()
{
    std::istringstream one("one");
    std::ostringstream two("two");

    std::cout << "Before swap: one = " << std::quoted(one.str())
              << ", two = " << std::quoted(two.str()) << ".\n";

    one.rdbuf()->swap(*two.rdbuf());

    std::cout << "After  swap: one = " << std::quoted(one.str())
              << ", two = " << std::quoted(two.str()) << ".\n";
}
```

Output:

```text
Before swap: one = "one", two = "two".
After  swap: one = "two", two = "one".
```

### See also

- [(constructor)](https://en.cppreference.com/w/cpp/io/basic_stringbuf/basic_stringbuf): constructs a `basic_stringbuf` object (public member function)
- [`swap`](https://en.cppreference.com/w/cpp/io/basic_stringstream/swap) (C++11): swaps two string streams (public member function of `std::basic_stringstream<CharT,Traits,Allocator>`)

---
Attribution: content from https://en.cppreference.com/w/cpp/io/basic_stringbuf/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.