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++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 432 | C++98 | 不清楚透過 overflow() 新增的字元 是否被視為輸入序列的一部分 |
已明確 |
[編輯] 參閱
[virtual] |
從關聯輸入序列中讀取字元到獲取區 ( std::basic_streambuf<CharT,Traits> 的虛保護成員函式) |
[virtual] |
從關聯檔案讀取 ( std::basic_filebuf<CharT,Traits> 的虛保護成員函式) |
[virtual] |
從輸入序列中讀取一個字元,但不推進下一個指標 ( std::strstreambuf 的虛保護成員函式) |
從輸入序列中讀取一個字元而不推進序列 ( std::basic_streambuf<CharT,Traits> 的公有成員函式) |