Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | io‎ | basic stringbuf
 
 
Input/output library
I/O manipulators
C-style I/O
Buffers
(deprecated in C++98)
Streams
Abstractions
File I/O
String I/O
Array I/O
(deprecated in C++98)
(deprecated in C++98)
(deprecated in C++98)
Synchronized Output
Types
Error category interface
(C++11)
 
 
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)

Contents

[edit] Parameters

rhs - another basic_stringbuf

[edit] Return value

(none)

[edit] Exceptions

(none)

(since C++11)
(until C++20)
noexcept specification:  
noexcept(std::allocator_traits<Allocator>::propagate_on_container_swap::value
|| std::allocator_traits<Allocator>::is_always_equal::value)
(since C++20)

[edit] Notes

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

[edit] Example

#include <sstream>
#include <string>
#include <iostream>
 
int main()
{
 
    std::istringstream one("one");
    std::ostringstream two("two");
 
    std::cout << "Before swap, one = \"" << one.str() << '"'
              << " two = \"" << two.str() << "\"\n";
 
    *one.rdbuf()->swap(*two.rdbuf());
 
    std::cout << "Before swap, one = \"" << one.str() << '"'
              << " two = \"" << two.str() << "\"\n";
}

Output:

Before swap, one = "one" two = "two"
Before swap, one = "two" two = "one"

[edit] See also

constructs a basic_stringbuf object
(public member function) [edit]
(C++11)
swaps two string streams
(public member function of std::basic_stringstream<CharT,Traits,Allocator>) [edit]