basic_stringstream() : basic_stringstream(std::ios_base::in | std::ios_base::out) { } | (1) | (since C++11) |
(2) | ||
explicit basic_stringstream( std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out ); | (until C++11) | |
explicit basic_stringstream( std::ios_base::openmode mode ); | (since C++11) | |
explicit basic_stringstream( const std::basic_string<CharT,Traits,Allocator>& str, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out ); | (3) | |
basic_stringstream( basic_stringstream&& other ); | (4) | (since C++11) |
basic_stringstream( std::ios_base::openmode mode, const Allocator& a ); | (5) | (since C++20) |
explicit basic_stringstream( std::basic_string<CharT,Traits,Allocator>&& str, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out ); | (6) | (since C++20) |
template<class SAlloc> basic_stringstream( const std::basic_string<CharT,Traits,SAlloc>& str, const Allocator& a ) : basic_stringstream(str, std::ios_base::in | std::ios_base::out, a) { } | (7) | (since C++20) |
template<class SAlloc> basic_stringstream( const std::basic_string<CharT,Traits,SAlloc>& str, std::ios_base::openmode mode, const Allocator& a ); | (8) | (since C++20) |
template<class SAlloc> explicit basic_stringstream( const std::basic_string<CharT,Traits,SAlloc>& str, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out ); | (9) | (since C++20) |
Constructs new string stream.
basic_stringbuf
object is constructed as basic_stringbuf<Char,Traits,Allocator>(mode)
.str
as initial contents of the underlying string device. The underlying basic_stringbuf
object is constructed as basic_stringbuf<Char,Traits,Allocator>(str, mode)
.other
using move semantics.basic_stringbuf
object is constructed as basic_stringbuf<Char,Traits,Allocator>(mode, a)
.str
. The underlying basic_stringbuf
object is constructed as basic_stringbuf<Char,Traits,Allocator>(std::move(str), mode)
.basic_stringbuf
object is constructed as basic_stringbuf<Char,Traits,Allocator>(str, std::ios_base::in | std::ios_base::out, a)
.basic_stringbuf
object is constructed as basic_stringbuf<Char,Traits,Allocator>(str, mode, a)
.basic_stringbuf
object is constructed as basic_stringbuf<Char,Traits,Allocator>(str, mode)
. This overload participates in overload resolution only if SAlloc
and Allocator
are not the same type.str | - | string to use as initial contents of the string stream | ||||||||||||||
a | - | allocator used for allocating the contents of the string stream | ||||||||||||||
mode | - | specifies stream open mode. It is bitmask type, the following constants are defined:
|
||||||||||||||
other | - | another string stream to use as source |
Construction of one-off basic_stringstream
objects in a tight loop, such as when used for string conversion, may be significantly more costly than calling str
to reuse the same object.
#include <iostream> #include <sstream> int main() { // default constructor (input/output stream) std::stringstream buf1; buf1 << 7; int n = 0; buf1 >> n; std::cout << "buf1 = " << buf1.str() << " n = " << n << '\n'; // input stream std::istringstream inbuf("-10"); inbuf >> n; std::cout << "n = " << n << '\n'; // output stream in append mode (C++11) std::ostringstream buf2("test", std::ios_base::ate); buf2 << '1'; std::cout << buf2.str() << '\n'; }
Output:
buf1 = 7 n = 7 n = -10 test1
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
P0935R0 | C++11 | default constructor was explicit | made implicit |
gets or sets the contents of underlying string device object (public member function) |
|
constructs a basic_stringbuf object (public member function of std::basic_stringbuf<CharT,Traits,Allocator> ) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/io/basic_stringstream/basic_stringstream