命名空间
变体
操作

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 以指示失败。

内容

[编辑] 参数

(无)

[编辑] 返回值

获取区域中的下一个字符,在成功时为 (unsigned char)(*gptr()),在失败时为 EOF

[编辑] 示例

#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

[编辑] 另请参阅

[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> 的公共成员函数) [编辑]