名稱空間
變體
操作

std::strstreambuf::underflow

來自 cppreference.com
< cpp‎ | io‎ | strstreambuf
protected:
virtual int_type underflow();
(C++98 起棄用)
(C++26 中移除)

從緩衝區的獲取區讀取下一個字元。

如果輸入序列有一個可用的讀取位置(gptr() < egptr()),則返回 (unsigned char)(*gptr())

否則,如果 pptr() 不為空且 pptr() > egptr()(存在一個放置區且其位於獲取區之後),則透過將 egptr() 增加到 gptr()pptr() 之間的某個值來擴充套件獲取區的末尾,以包含最近寫入放置區的字元,然後返回 (unsigned char)(*gptr())

否則,返回 EOF 以指示失敗。

目錄

[edit] 引數

(無)

[edit] 返回值

成功時,返回獲取區中的下一個字元,即 (unsigned char)(*gptr());失敗時返回 EOF

[edit] 示例

#include <iostream>
#include <strstream>
 
struct mybuf : std::strstreambuf
{
    int_type overflow(int_type c) 
    {
        std::cout << "Before overflow(): size of the get area is " << egptr()-eback()
                  << " size of the put area is " << epptr()-pbase() << '\n';
        int_type rc = std::strstreambuf::overflow(c);
        std::cout << "After overflow(): size of the get area is " << egptr()-eback()
                  << " size of the put area is " << epptr()-pbase() << '\n';
        return rc;
    }
 
    int_type underflow() 
    {
        std::cout << "Before underflow(): size of the get area is " << egptr()-eback()
                  << " size of the put area is " << epptr()-pbase() << '\n';
        int_type ch = std::strstreambuf::underflow();
        std::cout << "After underflow(): size of the get area is " << egptr()-eback()
                  << " size of the put area 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; // read-write dynamic strstreambuf
    std::iostream stream(&sbuf);
 
    int n;
    stream >> n;
    stream.clear();
    stream << "123";
    stream >> n;
    std::cout << n << '\n';
}

可能的輸出

Before underflow(): size of the get area is 0 size of the put area is 0
After underflow(): size of the get area is 0 size of the put area is 0
underflow() returns EOF
Before overflow(): size of the get area is 0 size of the put area is 0
After overflow(): size of the get area is 0 size of the put area is 32
Before underflow(): size of the get area is 0 size of the put area is 32
After underflow(): size of the get area is 3 size of the put area is 32
underflow() returns '1'
Before underflow(): size of the get area is 3 size of the put area is 32
After underflow(): size of the get area is 3 size of the put area is 32
underflow() returns EOF
123

[edit] 參閱

[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_istream<CharT,Traits> 的公有成員函式) [編輯]