首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::basic_stringbuf::underflow

protected: virtual int_type underflow()

?

?

从缓冲区的GET区域读取下一个字符。

具体而言:

1%29如果输入序列有读取位置可用%28egptr() > gptr()%29,回报Traits::to_int_type(*gptr())

2%29否则,如果pptr() > egptr()%28有些字符从上次插入到流中overflow()变了egptr()%29然后通过更改GET区域的末尾,将最近插入的字符包括在内。egptr()平等pptr(),然后返回Traits::to_int_type(*gptr())

3%29否则,返回Traits::eof()...

缓冲区中任何已初始化的字符,无论该字符来自于传递给构造函数的字符串还是由overflow(),被认为是输入序列的一部分。

参数

%280%29

返回值

Traits::to_int_type(*gptr())%28在GET区域%29中读取的下一个字符(如果成功),或Traits::eof()万一失败。

二次

代码语言:javascript
复制
#include <sstream>
#include <iostream>
 
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';
    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';
}

二次

可能的产出:

二次

代码语言:javascript
复制
Before underflow(): get area size is 3  put area size is 3
After underflow(): get area size is 3  put area size is 3
underflow() returns EOF
123
Before overflow(): get area size is 3  the put area size is 3
After overflow(): get area size is 4  put area size is 35
Before underflow(): get area size is 4  put area size is 35
After underflow(): get area size is 6  put area size is 35
underflow() returns '5'
Before underflow(): get area size is 6  put area size is 35
After underflow(): get area size is 6  put area size is 35
underflow() returns EOF
456

二次

另见

underflow virtual

reads characters from the associated input sequence to the get area (virtual protected member function of std::basic_streambuf)

underflow virtual

reads from the associated file (virtual protected member function of std::basic_filebuf)

underflow virtual

reads a character from the input sequence without advancing the next pointer (virtual protected member function of std::strstreambuf)

sgetc

reads one character from the input sequence without advancing the sequence (public member function of std::basic_streambuf)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com