std::strstreambuf:: overflow
From cppreference.net
<
cpp
|
io
|
strstreambuf
|
protected
:
virtual int_type overflow ( int_type c = EOF ) ; |
(C++98 中已弃用)
(C++26 中移除) |
|
将字符 c 追加到缓冲区的放置区域,并在可能时重新分配内存。
1)
如果
c
==
EOF
,则不执行任何操作。
2)
否则,若放置区域存在可写入位置(
pptr
(
)
<
epptr
(
)
),则按
*
pptr
(
)
++
=
c
的方式存储该字符。
3)
否则,如果流缓冲区模式不是动态的或流缓冲区当前被冻结,则函数失败并返回
EOF
。
4)
否则,该函数会重新分配(或初始分配)一个动态数组,其大小足以容纳当前动态数组(如果存在)的内容加上至少一个额外的写入位置。如果在构造函数中使用了指向分配函数
palloc
的指针,则调用该函数并传入
(
*
palloc
)
(
n
)
其中
n
是要分配的字节数,否则使用
new
char
[
n
]
。如果在构造函数中使用了指向释放函数
pfree
的指针,则调用该函数并传入
(
*
pfree
)
(
p
)
来释放之前的数组(如果需要),否则使用
delete
[
]
p
。如果分配失败,该函数将执行失败并返回
EOF
。
目录 |
参数
| c | - | 要存储到放置区域的字符 |
返回值
如果 c == EOF ,则返回某个非 EOF 的值。否则,成功时返回 ( unsigned char ) ( c ) ,失败时返回 EOF 。
示例
运行此代码
#include <iostream> #include <strstream> struct mybuf : std::strstreambuf { int_type overflow(int_type c) { std::cout << "Before overflow(): size of the put area is " << epptr()-pbase() << " with " << epptr()-pptr() << " write positions available\n"; int_type rc = std::strstreambuf::overflow(c); std::cout << "After overflow(): size of the put area is " << epptr()-pbase() << " with " << epptr()-pptr() << " write positions available\n"; return rc; } }; int main() { mybuf sbuf; // 可读写的动态 strstreambuf std::iostream stream(&sbuf); stream << "足够长的字符串以溢出初始分配,至少在" << "某些系统上会如此。"; }
可能的输出:
Before overflow(): size of the put area is 16 with 0 write positions available After overflow(): size of the put area is 32 with 15 write positions available Before overflow(): size of the put area is 32 with 0 write positions available After overflow(): size of the put area is 64 with 31 write positions available Before overflow(): size of the put area is 64 with 0 write positions available After overflow(): size of the put area is 128 with 63 write positions available
参见
|
[virtual]
|
将字符从输出区写入到关联的输出序列
(
std::basic_streambuf<CharT,Traits>
的虚受保护成员函数)
|
|
[virtual]
|
向输出序列追加字符
(
std::basic_stringbuf<CharT,Traits,Allocator>
的虚受保护成员函数)
|
|
[virtual]
|
将字符从输出区写入到关联的文件
(
std::basic_filebuf<CharT,Traits>
的虚受保护成员函数)
|
|
写入一个字符到输出区并推进下一个指针
(
std::basic_streambuf<CharT,Traits>
的公开成员函数)
|
|
|
插入一个字符
(
std::basic_ostream<CharT,Traits>
的公开成员函数)
|