std::ostrst...::ostrstream(3)
π NAME π
okay so this is the std::ostrst...::ostrstream(3) page from the C++ Standard Libary (yes that spelling, we're keeping it, no notes). The name is std::ostrstream::ostrstream β constructor with the same name as the class, main character energy fr fr.
β¨ Synopsis β¨
ostrstream(); (1)
(deprecated in C++98)
(removed in C++26)
ostrstream( char* s, int n, std::ios_base::openmode mode = std::ios_base::out ); (2)
(deprecated in C++98)
(removed in C++26)
This constructs a new output strstream and its underlying std::strstreambuf. π
1) Default-constructs the underlying std::strstreambuf, which creates a dynamically growing buffer, and initializes the base class with the address of the strstreambuf member. cute.
2) Initializes the base class with the address of the underlying std::strstreambuf member, which is initialized in one of the two possible ways β both of which write to a user-provided fixed-size array:
- a) if the
appbit is not set inmode, it constructs the buffer by callingstrstreambuf(s, n, s). The behavior is undefined if there are less thannelements in the array whose first element is pointed to bys.
π¨ undefined behavior alert: less than n elements in that array? bestie no. That's the danger zone.
- b) if the
appbit is set inmode, it constructs the buffer by callingstrstreambuf(s, n, s + std::strlen(s)). The behavior is undefined if there are less thannelements in the array whose first element is pointed to bys, or if the array does not contain a valid null-terminated character sequence.
π another undefined behavior warning: not enough elements OR no valid null-terminated character sequence in the array = nope, don't do it.
πΈ Parameters πΈ
sβ char array to use as the output buffer.nβ size of the array to be used as the output buffer.modeβ specifies stream open mode. It is a bitmask type, and the following constants are defined (although onlyappis used):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
π Example π
// Run this code
#include <iostream>
#include <strstream>
int main()
{
std::ostrstream s1; // dynamic buffer
s1 << 1 << ' ' << 3.14 << " example\n" << std::ends;
std::cout << s1.str();
s1.freeze(false);
char arr[15] = "Hello";
std::ostrstream s2(arr, sizeof arr, std::ios_base::app);
s2 << ", world!" << std::ends;
std::cout << s2.str() << '\n';
std::cout << arr << '\n'; // streams use the provided arrays
}
π Output π
1 3.14 example
Hello, world!
Hello, world!
π See also π
- constructor β constructs a
strstreambufobject (public member function ofstd::strstreambuf) - constructor β constructs an
istrstreamobject, optionally allocating the buffer (public member function ofstd::istrstream) - constructor β constructs a
strstreamobject, optionally allocating the buffer (public member function ofstd::strstream)
xoxo bestie π source: http://cppreference.com; version/date: 2024.06.10; page: std::ostrst...::ostrstream(3). slay.
π¦ 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