std::basic_...stringstream(3)
π std::basic_...stringstream(3) β C++ Standard Libary, section 3 π
okay bestie, this is the C++ Standard Libary manual page for std::basic_ostringstream::basic_ostringstream, and we're gonna make it make sense. no cap. π
π NAME π
std::basic_ostringstream::basic_ostringstream β std::basic_ostringstream::basic_ostringstream
β¨ Synopsis β¨
explicit basic_ostringstream( std::ios_base::openmode mode =
(until C++11)
std::ios_base::out );
explicit basic_ostringstream( std::ios_base::openmode mode );
(1) (since C++11)
basic_ostringstream() (2)
(since C++11)
: basic_ostringstream(std::ios_base::out) {}
explicit basic_ostringstream
( const std::basic_string<CharT, Traits, Allocator>& str, (3)
std::ios_base::openmode mode =
std::ios_base::out );
explicit basic_ostringstream
( std::basic_string<CharT, Traits, Allocator>&& str, (4)
(since C++20)
std::ios_base::openmode mode =
std::ios_base::out );
basic_ostringstream( std::ios_base::openmode mode, const (5)
(since C++20)
Allocator& a );
template< class SAlloc >
basic_ostringstream( const std::basic_string<CharT, Traits,
SAlloc>& str, (6)
(since C++20)
std::ios_base::openmode mode, const
Allocator& a );
template< class SAlloc >
basic_ostringstream( const std::basic_string<CharT, Traits, (1)
SAlloc>& str, (7)
(since C++20)
const Allocator& a )
: basic_ostringstream(str, std::ios_base::out, a) {}
template< class SAlloc >
explicit basic_ostringstream
( const std::basic_string<CharT, Traits, SAlloc>& str, (8)
(since C++20)
std::ios_base::openmode mode =
std::ios_base::out );
template< class StringViewLike >
explicit basic_ostringstream
( const StringViewLike& t, (9)
(since C++26)
std::ios_base::openmode mode =
std::ios_base::out );
template< class StringViewLike >
basic_ostringstream( const StringViewLike& t,
(10) (since C++26)
std::ios_base::openmode mode, const
Allocator& a );
template< class StringViewLike >
basic_ostringstream( const StringViewLike& t, const
(11) (since C++26)
Allocator& a );
basic_ostringstream( basic_ostringstream&& other );
(12) (since C++11)
Constructs new string stream.
Given
base_typeasstd::basic_ostream<CharT, Traits>, andbuf_typeasstd::basic_stringbuf<CharT, Traits, Allocator>,
the std::basic_ostream base and the exposition-only data member sb are initialized as follows.
- (1) β base: (empty);
sb:buf_type(mode | std::ios_base::out) - (2) β base: (empty);
sb:buf_type(std::ios_base::out) - (3) β base: (empty);
sb:buf_type(str, mode | std::ios_base::out) - (4) β base: (empty);
sb:buf_type(std::move(str), mode | std::ios_base::out) - (5) β base:
base_type(std::addressof(sb))^[1];sb:buf_type(mode | std::ios_base::out, a) - (6) β base: (empty);
sb:buf_type(str, mode | std::ios_base::out, a) - (7) β base: (empty);
sb:buf_type(str, std::ios_base::out, a) - (8) β base: (empty);
sb:buf_type(str, mode | std::ios_base::out) - (9) β base:
std::addressof(sb);sb:{t, mode | std::ios_base::out, Allocator()} - (10) β base: (empty);
sb:{t, mode | std::ios_base::out, a} - (11) β base: (empty);
sb:{t, std::ios_base::out, a} - (12) β base: move constructed from
other's std::basic_ostream base;sb: move constructed fromother.sb
- β The
std::basic_iostreambase was intialized withbase_type(&sb)(for overloads(1,3)) until C++11.
8)This overload participates in overload resolution only ifstd::is_same_v<SAlloc, Allocator>is false.9-11)These overloads participate in overload resolution only ifstd::is_convertible_v<const StringViewLike&, std::basic_string_view<CharT, Traits>>is true.
πΈ Parameters πΈ
strβ string to use as initial contents of the string streamtβ an object (convertible tostd::basic_string_view) to use as initial contents of the string streamaβ allocator used for allocating the contents of the string streammodeβ 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_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:
__cpp_lib_sstream_from_string_viewβ value202306L; Std(C++26); feature: Interfacingstd::stringstreamswithstd::string_view,(9-11)
π Example π
// Run this code
#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; Behavior as published: the default constructor wasexplicit; Correct behavior: made implicit
π See also π
strβ gets or sets the contents of underlying string device object (public member function)- constructor β constructs a
basic_stringbufobject (public member function ofstd::basic_stringbuf<CharT,Traits,Allocator>)
mwah π this page came from http://cppreference.com, version 2024.06.10, and itβs std::basic_...stringstream(3) β now go slay those streams, bestie π«Άβ¨
π¦ 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