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

std::basic_stringbuf::overflow

protected: virtual int_type overflow ( int_type c = Traits::eof() );

?

?

附加字符c输出字符序列。

如果c是文件末尾的指示器。traits::eq_int_type(c,traits::eof()) == true,那么就没有字符可以附加了。函数不执行任何操作,并返回一个未指定的值。traits::eof()...

否则,如果输出序列具有可用的写入位置,或者此函数可以成功地使写入位置可用,则调用sputc(c)和回报c...

如果字符串为输出%28打开,则此函数可以使写入位置可用。mode & ios_base::out) != 0%29:在这种情况下,它重新分配%28或最初分配%29缓冲区足够大,足以容纳整个当前缓冲区加上至少多一个字符。如果字符串f也打开以输入%28(mode & ios_base::in) != 0,然后overflow还通过移动来增加GET区域的大小。egptr()在新的书写位置上指点。

参数

%280%29

返回值

Traits::eof()为了表示失败,c如果字符c已成功追加,或其他值Traits::eof()如果被调用Traits::eof()作为争论。

注记

此函数与典型的overflow()将缓冲区的内容移动到关联的字符序列,因为对于std::basic_stringbuf,缓冲器和相关序列是一个和相同的。

在用于执行此示例的实现中,溢出%28%29会将PUT区域过度分配到512字节:对str%28%29的调用只会返回四个初始化字节,但是对spac%28%29的下一个508调用将不需要对溢出%28%29进行新的调用。

二次

代码语言: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 = EOF) override
    {
        std::cout << "stringbuf::overflow('" << char(c) << "') called\n"
                  << "Before: size of get area: " << egptr() - eback() << '\n'
                  << "        size of put area: " << epptr() - pbase() << '\n';
        int_type ret = std::stringbuf::overflow(c);
        std::cout << "After : size of get area: " << egptr() - eback() << '\n'
                  << "        size of put area: " << epptr() - pbase() << '\n';
        return ret;
    }
};
 
int main()
{
    std::cout << "read-write stream:\n";
    mybuf sbuf("   "); // read-write stream
    std::iostream stream(&sbuf);
    stream << 1234;
    std::cout << sbuf.str() << '\n';
 
    std::cout << "\nread-only stream:\n";
    mybuf ro_buf("   ", std::ios_base::in); // read-only stream
    std::iostream ro_stream(&ro_buf);
    ro_stream << 1234;
 
    std::cout << "\nwrite-only stream:\n";
    mybuf wr_buf("   ", std::ios_base::out); // write-only stream
    std::iostream wr_stream(&wr_buf);
    wr_stream << 1234;
}

二次

可能的产出:

二次

代码语言:javascript
复制
read-write stream:
stringbuf::overflow('4') called
Before: size of get area: 3
        size of put area: 3
After : size of get area: 4
        size of put area: 512
1234
 
read-only stream:
stringbuf::overflow('1') called
Before: size of get area: 3
        size of put area: 0
After : size of get area: 3
        size of put area: 0
 
write-only stream:
stringbuf::overflow('4') called
Before: size of get area: 0
        size of put area: 3
After : size of get area: 0
        size of put area: 512

二次

另见

overflow virtual

writes characters to the associated output sequence from the put area (virtual protected member function of std::basic_streambuf)

underflow virtual

returns the next character available in the input sequence (virtual protected member function)

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

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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com