std::basic_...stringstream(3)
std::basic_stringstream::basic_stringstream π
(from the C++ Standard Library manual β yes bestie, we're doing constructors today π«Ά)
π NAME π
std::basic_stringstream::basic_stringstream β the constructor for std::basic_stringstream, aka how your string stream is born. π
β¨ Synopsis β¨
Okay so there are twelve overloads and we are listing every single one, no cap. π
// (1)
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)
// (2) (since C++11)
basic_stringstream()
: basic_stringstream(std::ios_base::in |
std::ios_base::out) {}
// (3)
explicit basic_stringstream
( const std::basic_string<CharT, Traits, Allocator>& str,
std::ios_base::openmode mode =
std::ios_base::in | std::ios_base::out );
// (4) (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 );
// (5) (since C++20)
basic_stringstream( std::ios_base::openmode mode, const
Allocator& a );
// (6) (since C++20)
template< class SAlloc >
basic_stringstream( const std::basic_string<CharT, Traits,
SAlloc>& str,
std::ios_base::openmode mode, const
Allocator& a );
// (7) (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) {}
// (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++26)
template< class StringViewLike >
explicit basic_stringstream
( const StringViewLike& t,
std::ios_base::openmode mode =
std::ios_base::in | std::ios_base::out );
// (10) (since C++26)
template< class StringViewLike >
basic_stringstream( const StringViewLike& t,
std::ios_base::openmode mode, const
Allocator& a );
// (11) (since C++26)
template< class StringViewLike >
basic_stringstream( const StringViewLike& t, const Allocator&
a );
// (12) (since C++11)
basic_stringstream( basic_stringstream&& other );
Constructs new string stream. π«§
Given
base_typeasstd::basic_iostream<CharT, Traits>, andbuf_typeasstd::basic_stringbuf<CharT, Traits, Allocator>,
the std::basic_iostream base and the exposition-only data member sb are initialized as follows. π
- (1) β
sb:buf_type(mode) - (2) β
sb:buf_type(std::ios_base::in | std::ios_base::out) - (3) β
sb:buf_type(str, mode) - (4) β
std::basic_iostreambase:base_type(std::addressof(sb))[1];sb:buf_type(std::move(str), mode) - (5) β
sb:buf_type(mode, a) - (6) β
sb:buf_type(str, mode, a) - (7) β
sb:buf_type(str, std::ios_base::in | std::ios_base::out, a) - (8) β
sb:buf_type(str, mode) - (9) β
sb:{t, mode, Allocator()} - (10) β
std::basic_iostreambase:std::addressof(sb);sb:{t, mode, a} - (11) β
sb:{t, std::ios_base::in | std::ios_base::out, a} - (12) β
std::basic_iostreambase: move constructed fromother'sstd::basic_iostreambase;sb: move constructed fromother.sb
[1] Thestd::basic_iostreambase was initialized withbase_type(&sb)(for overloads(1,3)) until C++11.
- (8) This overload participates in overload resolution only if
std::is_same_v<SAlloc, Allocator>isfalse. - (9-11) These overloads participate in overload resolution only if
std::is_convertible_v<const StringViewLike&, std::basic_string_view<CharT, Traits>>istrue.
πΈ Parameters πΈ
strβ string to use as initial contents of the string stream.tβ an object (convertible tostd::basic_string_view) 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 aBitmaskType, the following constants are defined:appβ seek to the end of stream before each writebinaryβ open in binary modeinβ open for readingoutβ open for writingtruncβ discard the contents of the stream when openingateβ seek to the end of stream immediately after opennoreplace(C++23) β open in exclusive mode
otherβ another string stream to use as source.
π¦ Notes π¦
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. So likeβ¦ don't spam constructors if you can just reuse, bestie. πΈ
β Feature-test macro β
__cpp_lib_sstream_from_string_viewβ value202306Lβ std(C++26)β feature: Interfacingstd::stringstreamswithstd::string_view,(9-11).
π Example π
Run this code, bestie:
#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
π©Ή Defect reports π©Ή
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- P0935R0 β applied to C++11 β the default constructor was
explicit, made implicit.
π See also π
strβ gets or sets the contents of underlying string device object (public member function).constructorβ constructs abasic_stringbufobject (public member function ofstd::basic_stringbuf<CharT,Traits,Allocator>).
xoxo bestie π source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::basic_...stringstream(3) β¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms