std::strstreambuf::overflow
来自cppreference.com
< 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) 调用该函数,否则用 new char[n],其中 n
为要分配的字节数。若构造函数中使用了指向解分配函数的指针 pfree
,则以 (*pfree)(p) 调用该函数分配先前的数组,否则用 delete[] p,若需要解分配。若分配失败,则函数失败并返回 EOF 。目录 |
[编辑]参数
c | - | 要存储于放置区的字符 |
[编辑]返回值
若 c ==EOF,则返回异于 EOF 的某值。否则,成功时返回 (unsignedchar)(c),失败时返回 EOF。
[编辑]示例
运行此代码
#include <iostream>#include <strstream> struct mybuf :std::strstreambuf{ int_type overflow(int_type c){std::cout<<"overflow() 前:放置区的大小为 "<< epptr()-pbase()<<",有 "<< epptr()-pptr()<<" 个可用写入位置\n"; int_type rc = std::strstreambuf::overflow(c);std::cout<<"overflow() 后:放置区的大小为 "<< epptr()-pbase()<<",有 "<< epptr()-pptr()<<" 个可用写入位置\n";return rc;}}; int main(){ mybuf sbuf;// 读写动态 strstreambufstd::iostream stream(&sbuf); stream <<"Sufficiently long string to overflow the initial allocation, at least "<<" on some systems.";}
可能的输出:
overflow() 前:放置区的大小为 16,有 0 个可用写入位置 overflow() 后:放置区的大小为 32,有 15 个可用写入位置 overflow() 前:放置区的大小为 32 个 0 个可用写入位置 overflow() 后:放置区的大小为 64 个 31 个可用写入位置 overflow() 前:放置区的大小为 64 个 0 个可用写入位置 overflow() 后:放置区的大小为 128 个 63 个可用写入位置
[编辑]参阅
[虚] | 从放置区写入字符到关联的输出序列 ( std::basic_streambuf<CharT,Traits> 的虚受保护成员函数) |
[虚] | 后附字符到输出序列 ( std::basic_stringbuf<CharT,Traits,Allocator> 的虚受保护成员函数) |
[虚] | 从放置区写字符到关联的文件 ( std::basic_filebuf<CharT,Traits> 的虚受保护成员函数) |
写一个字符到放置区域,并推进下一位置指针 ( std::basic_streambuf<CharT,Traits> 的公开成员函数) | |
插入字符 ( std::basic_ostream<CharT,Traits> 的公开成员函数) |