std::strstreambuf::underflow
来自cppreference.com
< cpp | io | strstreambuf
protected: virtual int_type underflow(); | (C++98 弃用) (C++26 移除) | |
从缓冲区的获取区读取下个字符。
若输入序列拥有可用读位置(gptr()< egptr()),则返回 (unsignedchar)(*gptr())。
否则,若 pptr() 非空且 pptr()> egptr()(存在放置区,且它位于获取区之后),则以增加 egptr() 到 gptr() 和 pptr() 间的某个值扩展获取区的结尾,以包含最近写入到放置区中的字符,然后返回 (unsignedchar)(*gptr()) 。
否则,返回 EOF 以指示失败。
目录 |
[编辑]参数
(无)
[编辑]返回值
成功时为获取区中下个字符 (unsignedchar)(*gptr()) ,失败时为 EOF 。
[编辑]示例
运行此代码
#include <iostream>#include <strstream> struct mybuf :std::strstreambuf{ int_type overflow(int_type c){std::cout<<"overflow() 前:获取区的大小为 "<< egptr()-eback()<<",放置区的大小为 "<< epptr()-pbase()<<'\n'; int_type rc = std::strstreambuf::overflow(c);std::cout<<"overflow() 后:获取区的大小为 "<< egptr()-eback()<<",放置区的大小为 "<< epptr()-pbase()<<'\n';return rc;} int_type underflow(){std::cout<<"underflow() 前:获取区的大小为 "<< egptr()-eback()<<",放置区的大小为 "<< epptr()-pbase()<<'\n'; int_type ch = std::strstreambuf::underflow();std::cout<<"underflow() 后:获取区的大小为 "<< egptr()-eback()<<",放置区的大小为 "<< epptr()-pbase()<<'\n';if(ch ==EOF)std::cout<<"underflow() 返回 EOF\n";elsestd::cout<<"underflow() 返回 '"<<char(ch)<<"'\n";return ch;}}; int main(){ mybuf sbuf;// 读写动态 strstreambufstd::iostream stream(&sbuf); int n; stream >> n; stream.clear(); stream <<"123"; stream >> n;std::cout<< n <<'\n';}
可能的输出:
underflow() 前:获取区的大小为 0,放置区的大小为 0 underflow() 后:获取区的大小为 0,放置区的大小为 0 underflow() 返回 EOF overflow() 前:获取区的大小为 0,放置区的大小为 0 overflow() 后:获取区的大小为 0,放置区的大小为 32 underflow() 前:获取区的大小为 0,放置区的大小为 32 underflow() 后:获取区的大小为 3,放置区的大小为 32 underflow() 返回 '1' underflow() 前:获取区的大小为 3,放置区的大小为 32 underflow() 后:获取区的大小为 3,放置区的大小为 32 underflow() 返回 EOF 123
[编辑]参阅
[虚] | 从关联输入序列读取字符到获取区 ( std::basic_streambuf<CharT,Traits> 的虚受保护成员函数) |
[虚] | 返回输入序列中可用的下一字符 ( std::basic_stringbuf<CharT,Traits,Allocator> 的虚受保护成员函数) |
[虚] | 从关联文件读取 ( std::basic_filebuf<CharT,Traits> 的虚受保护成员函数) |
从输入序列读取一个字符,而不推进序列 ( std::basic_streambuf<CharT,Traits> 的公开成员函数) | |
提取字符 ( std::basic_istream<CharT,Traits> 的公开成员函数) |