std::strstream::strstream
std::strstream::strstream
strstream( | (1) | |
---|---|---|
strstream(char* s, int n, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out | (2) | |
构造新的输入/输出strstream及其基础std::strstreambuf
...
1%29默认值-构造基础std::strstreambuf
,它创建一个动态增长的缓冲区,并使用strStrebuf成员的地址初始化基类。
2%29使用基础的地址初始化基类。std::strstreambuf
成员,它以两种可能的方式之一初始化,这两种方法都使用用户提供的固定大小数组:
A%29如果app
位未设置mode
,通过调用strstreambuf(s, n, s)
如果有少于n
数组中的第一个元素被s
b%29如果app
位mode
,通过调用strstreambuf(s, n, s +
std::strlen
(s))
如果有少于n
数组中的第一个元素被s
或者如果数组不包含有效的以空结尾的字符序列.
参数
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 only app is used): Constant Explanation app seek to the end of stream before each write binary open in binary mode in open for reading out open for writing trunc discard the contents of the stream when opening ate seek to the end of stream immediately after open |
Constant | Explanation | |
app | seek to the end of stream before each write | |
binary | open in binary mode | |
in | open for reading | |
out | open for writing | |
trunc | discard the contents of the stream when opening | |
ate | seek to the end of stream immediately after open |
例
二次
#include <iostream>
#include <strstream>
#include <string>
int main()
{
// dynamic buffer
std::strstream s1; // dynamic buffer
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 buffer
char 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";
}
二次
产出:
二次
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'
二次
另见
(constructor) | constructs a strstreambuf object (public member function of std::strstreambuf) |
---|---|
(constructor) | constructs an strstream, optionally allocating the buffer (public member function of std::istrstream) |
(constructor) | constructs an strstream, optionally allocating the buffer (public member function of std::ostrstream) |
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。