public member function
<sstream>

std::basic_stringbuf::basic_stringbuf

default (1)
explicit basic_stringbuf (ios_base::openmode which = ios_base::in | ios_base::out);
initialization (2)
explicit basic_stringbuf (const basic_string<char_type,traits_type,allocator_type>& str,                          ios_base::openmode which = ios_base::in | ios_base::out);
default (1)
explicit basic_stringbuf (ios_base::openmode which = ios_base::in | ios_base::out);
initialization (2)
explicit basic_stringbuf (const basic_string<char_type,traits_type,allocator_type>& str,                          ios_base::openmode which = ios_base::in | ios_base::out);
copy (3)
basic_stringbuf (const basic_stringbuf&) = delete;
move (4)
basic_stringbuf (basic_stringbuf&& x);
Construct object
Constructs a basic_stringbuf object:

(1) empty constructor (default constructor)
Constructs a basic_stringbuf object with an empty sequence as content, and argument which as open mode.
(2) initialization constructor
Constructs a basic_stringbuf object with a copy of str as content, and argument which as open mode.
(3) copy constructor (deleted)
Deleted (no copy constructor).
(4) move constructor
Acquires the contents of x.
x is left in an unspecified but valid state.
It is unspecified whether the internal sequence is the one in x before the call, or a copy of it. In any case, both objects use independent sequences after the call.

Parameters

str
A basic_string object with the same template parameters (charT, traits and Alloc), whose content is copied.
x
A basic_stringbuf object of the same type (with the same class template parameters charT, traits and Alloc), whose value is moved.
which
Open mode: Access given to the internal sequence of characters through the internal pointers that define the stream buffer's input sequence and output sequence. It is an object of type ios_base::openmode for which any combination of the following constant values is significant:
valuestands foraccess
ios_base::ininputThe sequence can be read: Members eback, gptr and egptr are maintained with values pointing to elements of the internal character sequence.
ios_base::outoutputThe sequence can be written: Members pbase, pptr and epptr are maintained with values pointing to elements of the internal character sequence.
Other values of type ios_base::openmode may also be specified, although whether they have an effect on basic_stringbuf objects depends on the library implementation.
valuestands foraccess
ios_base::ininputThe sequence can be read: Members eback, gptr and egptr are maintained with values pointing to elements of the internal character sequence.
ios_base::outoutputThe sequence can be written: Members pbase, pptr and epptr are maintained with values pointing to elements of the internal character sequence.
ios_base::ateat endThe put pointer (pptr) starts at the end of the sequence, and is reset to that position every time the contents are changed using member str.
Other values of type ios_base::openmode (such as ios_base::app) may also be specified, although whether they have an effect on basic_stringbuf objects depends on the library implementation.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// stringbuf example
#include <iostream>     // std::cout, std::ostream, std::hex
#include <sstream>      // std::stringbuf
#include <string>       // std::string

int main ()
{
  std::stringbuf buffer;      // empty stringbuf

  std::ostream os (&buffer);  // associate stream buffer to stream

  // mixing output to buffer with inserting to associated stream:
  buffer.sputn ("255 in hexadecimal: ",20);
  os << std::hex << 255;

  std::cout << buffer.str();

  return 0;
}

Output:
255 in hexadecimal: ff


Data races

The move constructor (4) modifies x.

Exception safety

Strong guarantee: if an exception is thrown, there are no side effects.

See also