std::basic_stringbuf<CharT,Traits,Allocator>::underflow
来自 cppreference.com
< cpp | io | basic stringbuf
protected: virtual int_type underflow() |
||
从缓冲区的获取区域读取下一个字符。
具体来说
1) 如果输入序列有可用的读取位置 (egptr() > gptr()),则返回 Traits::to_int_type(*gptr())
2) 否则,如果 pptr() > egptr()(自上次 overflow() 更改 egptr() 以来,已将一些字符插入到流中),则通过将 egptr() 更改为等于 pptr(),扩展获取区域的末尾以包含最近插入的字符,然后返回 Traits::to_int_type(*gptr())
3) 否则,返回 Traits::eof().
缓冲区中任何已初始化的字符,无论其源自构造函数中传递的字符串还是由 overflow() 追加,都被视为输入序列的一部分。
内容 |
[编辑] 参数
(无)
[编辑] 返回值
如果成功,则为 Traits::to_int_type(*gptr())(获取区域中要读取的下一个字符),如果失败,则为 Traits::eof().
[编辑] 示例
运行此代码
#include <iostream> #include <sstream> struct mybuf : std::stringbuf { mybuf(const std::string& new_str, std::ios_base::openmode which = std::ios_base::in | std::ios_base::out) : std::stringbuf(new_str, which) {} int_type overflow(int_type c) { std::cout << "Before overflow(): get area size is " << egptr() - eback() << ", the put area size is " << epptr() - pbase() << '\n'; int_type rc = std::stringbuf::overflow(c); std::cout << "After overflow(): get area size is " << egptr() - eback() << ", put area size is " << epptr() - pbase() << '\n'; return rc; } int_type underflow() { std::cout << "Before underflow(): get area size is " << egptr() - eback() << ", put area size is " << epptr() - pbase() << '\n'; int_type ch = std::stringbuf::underflow(); std::cout << "After underflow(): get area size is " << egptr() - eback() << ", put area size is " << epptr() - pbase() << '\n'; if (ch == EOF) std::cout << "underflow() returns EOF\n"; else std::cout << "underflow() returns '" << char(ch) << "'\n"; return ch; } }; int main() { mybuf sbuf("123"); // read-write stream std::iostream stream(&sbuf); int n; stream >> n; // calls sgetc() four times // three calls return the characters '1', '2', '3' // the fourth call, gptr() == egptr() and underflow() is called // underflow returns EOF std::cout << "n = " << n << '\n'; stream.clear(); // clear the eofbit stream << "123456"; // sputc() is called 6 times // first three calls store "123" in the existing buffer // 4th call finds that pptr() == epptr() and calls overflow() // overflow() grows the buffer and sets egptr() to 4 // 5th and 6th calls store '5' and '6', advancing pptr() stream >> n; // calls sgetc() 4 times // 1st call returns the '4' that was made available by overflow() // on the 2nd call, egptr() == egptr() and underflow() is called // underflow advances egptr() to equal pptr() (which is 6) // 3rd sgetc() returns '6' // 4th sgetc() finds gptr() == egptr(), calls underflow() // underflow() returns EOF std::cout << "n = " << n << '\n'; }
可能的输出
Before underflow(): get area size is 3, put area size is 15 After underflow(): get area size is 3, put area size is 15 underflow() returns EOF n = 123 Before underflow(): get area size is 3, put area size is 15 After underflow(): get area size is 6, put area size is 15 underflow() returns '4' Before underflow(): get area size is 6, put area size is 15 After underflow(): get area size is 6, put area size is 15 underflow() returns EOF n = 456
[编辑] 缺陷报告
以下更改行为的缺陷报告已追溯应用于以前发布的 C++ 标准。
DR | 应用于 | 发布的行为 | 正确的行为 |
---|---|---|---|
LWG 432 | C++98 | 不清楚由 overflow() 追加的字符 是否被认为是输入序列的一部分 |
已明确说明 |
[编辑] 另请参阅
[虚拟] |
从关联的输入序列读取字符到获取区域 ( std::basic_streambuf<CharT,Traits> 的虚拟受保护成员函数) |
[虚拟] |
从关联的文件读取 ( std::basic_filebuf<CharT,Traits> 的虚拟受保护成员函数) |
[虚拟] |
从输入序列读取一个字符,而不前进下一个指针 ( std::strstreambuf 的虚拟受保护成员函数) |
从输入序列读取一个字符,而不前进序列 ( std::basic_streambuf<CharT,Traits> 的公共成员函数) |