| I/O manipulators | ||||
| Print functions(C++23) | ||||
| C-style I/O | ||||
| Buffers | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++20) | ||||
| Streams | ||||
| Abstractions | ||||
| File I/O | ||||
| String I/O | ||||
| Array I/O | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
| Synchronized Output | ||||
(C++20) | ||||
| Types | ||||
| Error category interface | ||||
(C++11) | ||||
(C++11) |
| Member functions | ||||
strstream::strstream | ||||
strstream(); | (1) | (deprecated in C++98) (removed in C++26) |
strstream(char* s,int n,std::ios_base::openmode mode= std::ios_base::in|std::ios_base::out); | (2) | (deprecated in C++98) (removed in C++26) |
Constructs new input/output strstream and its underlyingstd::strstreambuf.
Contents |
| s | - | char array to use as the output buffer | ||||||||||||||||
| n | - | size of the array to be used for output | ||||||||||||||||
| mode | - | specifies stream open mode. It is a bitmask type, the following constants are defined (although onlyapp is used):
|
#include <iostream>#include <string>#include <strstream> int main(){// dynamic bufferstd::strstream s1; s1<<1<<' '<<3.14<<" example"<<std::ends;std::cout<<"Buffer holds: '"<< s1.str()<<"'\n"; s1.freeze(false); int n;double d;std::string w; s1>> n>> d>> w;std::cout<<"Read back: n = "<< n<<", d = "<< d<<", w = '"<< w<<"'\n"; // static bufferchar arr[20]="-1 -3.14 ";std::strstream s2(arr, sizeof arr,std::ios_base::app); s2<<"another"<<std::ends;std::cout<<"Buffer holds: '"<< s2.str()<<"'\n"; s2>> n>> d>> w;std::cout<<"Read back: n = "<< n<<", d = "<< d<<", w = '"<< w<<"'\n";}
Output:
Buffer holds: '1 3.14 example'Read back: n = 1, d = 3.14, w = 'example'Buffer holds: '-1 -3.14 another'Read back: n = -1, d = -3.14, w = 'another'
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 115 | C++98 | overload (2) only considered the case mode& app==0 ( == has higher precedence than&) | considers cases(mode& app)==0 and(mode& app)!=0 |
constructs astrstreambuf object(public member function of std::strstreambuf)[edit] | |
constructs anistrstream object, optionally allocating the buffer(public member function of std::istrstream)[edit] | |
constructs anostrstream object, optionally allocating the buffer(public member function of std::ostrstream)[edit] |