std::basic_ostringstream<CharT,Traits,Allocator>::basic_ostringstream
std::ios_base::out );
: basic_ostringstream(std::ios_base::out ) {}
( const std::basic_string <CharT, Traits, Allocator>& str,
std::ios_base::openmode mode =
( std::basic_string <CharT, Traits, Allocator>&& str,
std::ios_base::openmode mode =
basic_ostringstream( const std::basic_string <CharT, Traits, SAlloc>& str,
basic_ostringstream( const std::basic_string <CharT, Traits, SAlloc>& str,
const Allocator& a )
explicit basic_ostringstream
( const std::basic_string <CharT, Traits, SAlloc>& str,
std::ios_base::openmode mode =
explicit basic_ostringstream
( const StringViewLike& t,
std::ios_base::openmode mode =
basic_ostringstream( const StringViewLike& t,
basic_ostringstream( const StringViewLike& t, const Allocator& a );
Constructs new string stream.
Given
-
base_type
as std::basic_ostream <CharT, Traits>, and -
buf_type
as std::basic_stringbuf <CharT, Traits, Allocator>,
the std::basic_ostream base and the exposition-only data member sb
are initialized as follows.
Over load |
std::basic_ostream base | sb
|
---|---|---|
(1) | base_type(std::addressof (sb))[1] | buf_type(mode | std::ios_base::out ) |
(2) | buf_type(std::ios_base::out ) | |
(3) | buf_type(str, mode | std::ios_base::out ) | |
(4) | buf_type(std::move(str), mode | std::ios_base::out ) | |
(5) | buf_type(mode | std::ios_base::out, a) | |
(6) | buf_type(str, mode | std::ios_base::out, a) | |
(7) | buf_type(str, std::ios_base::out, a) | |
(8) | buf_type(str, mode | std::ios_base::out ) | |
(9) | std::addressof (sb) | {t, mode | std::ios_base::out, Allocator()} |
(10) | {t, mode | std::ios_base::out, a} | |
(11) | {t, std::ios_base::out, a} | |
(12) | move constructed from other's std::basic_ostream base | move constructed from other.sb |
- ↑ The std::basic_iostream base was intialized with base_type(&sb) (for overloads (1,3)) until C++11.
[edit] Parameters
[edit] Notes
Construction of one-off basic_ostringstream
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.
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_sstream_from_string_view |
202306L |
(C++26) | Interfacing std::stringstream s with std::string_view , (9-11) |
[edit] Example
#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
[edit] Defect reports
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 | the default constructor was explicit | made implicit |
[edit] See also
basic_stringbuf
object (public member function of
std::basic_stringbuf<CharT,Traits,Allocator>
) [edit]