“cpp/io/strstreambuf/overflow”的版本间的差异

来自cppreference.com
< cpp‎ | io‎ | strstreambuf
(link to ja)
第1行: 第1行:
 
{{cpp/io/strstreambuf/title | overflow }}
 
{{cpp/io/strstreambuf/title | overflow }}
 
{{cpp/io/strstreambuf/navbar}}
 
{{cpp/io/strstreambuf/navbar}}
{{ddcl |1=
+
{{ddcl|1=
 
protected:
 
protected:
 
virtual int_type overflow (int_type c = EOF);
 
virtual int_type overflow (int_type c = EOF);
 
}}
 
}}
  
 后附字符 {{tt|c}} 到缓冲的放置区,若可能则重分配。
+
 后附字符 {{|c}} 到缓冲 的放置区,若可能则重分配。
  
@1@若 {{tt|c{{==}}EOF}} ,则不做任何事
+
@1@ 若 {{tt|c {{==}} EOF}},则不做任何事
@2@否则,若放置区拥有可用的写位置( {{c|pptr() < epptr()}} ),则如同用 {{c|*pptr()++ {{=}} c}} 存储该字符
+
@2@ 否则,若放置区拥有可用的写位置({{c|pptr() < epptr()}}),则如同用 {{c|*pptr()++ {{=}} c}} 存储该字符
@3@否则,若流缓冲模式非动态,或流当前 冻结,则函数失败并返回 {{c|EOF}}
+
@3@ 否则,若流缓冲 模式非动态,或流当前 冻结,则函数失败并返回 {{c|EOF}}
@4@否则,函数重分配(或初始分配)足够 大以 保有当前动态数组(若存在)加至少一个额外写入位置的动态数组。若构造函数中使用了指向分配函数的指针 {{tt|palloc}} ,则以 {{c|(*palloc)(n)}} 调用该函数,否则用 {{c|new char[n]}} ,其中 {{tt|n}} 为要分配的字节数。若构造函数中使用了指向解分配函数的指针 {{tt|pfree}} ,则以 {{c|(*pfree)(p)}} 调用该函数分配先前的数组,否则用 {{c|delete[] p}} ,若需要解分配。若分配失败,则函数失败并返回 {{c|EOF}} 。
+
@4@ 否则,函数重分配(或初始分配) 足够保有当前动态数组(若存在)加至少一个额外写入位置的动态数组。若构造函数中使用了指向分配函数的指针 {{tt|palloc}},则以 {{c|(*palloc)(n)}} 调用该函数,否则用 {{c|new char[n]}},其中 {{tt|n}} 为要分配的字节数。若构造函数中使用了指向解分配函数的指针 {{tt|pfree}},则以 {{c|(*pfree)(p)}} 调用该函数分配先前的数组,否则用 {{c|delete[] p}},若需要解分配。若分配失败,则函数失败并返回 {{c|EOF}} 。
  
 
===参数===
 
===参数===
 
{{par begin}}
 
{{par begin}}
{{par | c |要存储于放置区的字符}}
+
{{par|c|要存储于放置区的字符}}
 
{{par end}}
 
{{par end}}
  
 
===返回值===
 
===返回值===
 若 {{c|c{{==}}EOF}} ,则返回异于 {{c|EOF}} 的某值。否则,成功时返回 {{c|(unsigned char)(c)}} ,失败时返回 {{lc|EOF}}
+
 若 {{c|c {{==}} EOF}},则返回异于 {{c|EOF}} 的某值。否则,成功时返回 {{c|(unsigned char)(c)}},失败时返回 {{lc|EOF}}。
  
 
===示例===
 
===示例===
 
{{example
 
{{example
|
+
|code=
| code=
+
#include <strstream>
+
 
#include <iostream>
 
#include <iostream>
 +
  
 
struct mybuf : std::strstreambuf
 
struct mybuf : std::strstreambuf
第32行: 第31行:
 
     int_type overflow(int_type c)  
 
     int_type overflow(int_type c)  
 
     {
 
     {
         std::cout << "Before overflow(): size of the put area is " << epptr()-pbase()
+
         std::cout << "overflow() " << epptr()-pbase()
                   << " with " << epptr()-pptr() << " write positions available\n";
+
                   << " " << epptr()-pptr() << " \n";
 
         int_type rc = std::strstreambuf::overflow(c);
 
         int_type rc = std::strstreambuf::overflow(c);
         std::cout << "After overflow(): size of the put area is " << epptr()-pbase()
+
         std::cout << "overflow() " << epptr()-pbase()
                   << " with " << epptr()-pptr() << " write positions available\n";
+
                   << " " << epptr()-pptr() << " \n";
 
         return rc;
 
         return rc;
 
     }
 
     }
第49行: 第48行:
 
           << " on some systems.";
 
           << " on some systems.";
 
}
 
}
| p=true
+
|p=true
| output=
+
|output=
Before overflow(): size of the put area is 16 with 0 write positions available
+
overflow() 16 0
After overflow(): size of the put area is 32 with 15 write positions available
+
overflow() 32 15
Before overflow(): size of the put area is 32 with 0 write positions available
+
overflow() 32 0
After overflow(): size of the put area is 64 with 31 write positions available
+
overflow() 64 31
Before overflow(): size of the put area is 64 with 0 write positions available
+
overflow() 64 0
After overflow(): size of the put area is 128 with 63 write positions available
+
overflow() 128 63
 
}}
 
}}
  
 
===参阅===
 
===参阅===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/io/basic_streambuf/dsc overflow}}
+
{{dsc inc|cpp/io/basic_streambuf/dsc overflow}}
{{dsc inc | cpp/io/basic_stringbuf/dsc overflow}}
+
{{dsc inc|cpp/io/basic_stringbuf/dsc overflow}}
{{dsc inc | cpp/io/basic_filebuf/dsc overflow}}
+
{{dsc inc|cpp/io/basic_filebuf/dsc overflow}}
{{dsc inc | cpp/io/basic_streambuf/dsc sputc}}
+
{{dsc inc|cpp/io/basic_streambuf/dsc sputc}}
{{dsc inc | cpp/io/basic_ostream/dsc put}}
+
{{dsc inc|cpp/io/basic_ostream/dsc put}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|en|ja}}
 
{{langlinks|en|ja}}

2023年12月16日 (六) 10:10的版本

protected:
virtual int_type overflow (int_type c = EOF);

后附字符 c 到缓冲区的放置区,若可能则重分配。

1)c == EOF,则不做任何事
2) 否则,若放置区拥有可用的写位置(pptr() < epptr()),则如同用 *pptr()++ = c 存储该字符。
3) 否则,若流缓冲区模式非动态,或流当前已冻结,则函数失败并返回 EOF
4) 否则,函数重分配(或初始分配)大小足够保有当前动态数组(若存在)加至少一个额外写入位置的动态数组。若构造函数中使用了指向分配函数的指针 palloc,则以 (*palloc)(n) 调用该函数,否则用 new char[n],其中 n 为要分配的字节数。若构造函数中使用了指向解分配函数的指针 pfree,则以 (*pfree)(p) 调用该函数分配先前的数组,否则用 delete[] p,若需要解分配。若分配失败,则函数失败并返回 EOF

目录

参数

c - 要存储于放置区的字符

返回值

c == EOF,则返回异于 EOF 的某值。否则,成功时返回 (unsigned char)(c),失败时返回 EOF

示例

#include <iostream>
#include <strstream>
 
struct mybuf : std::strstreambuf
{
    int_type overflow(int_type c) 
    {
        std::cout << "overflow() 前:放置区的大小为 " << epptr()-pbase()
                  << ",有 " << epptr()-pptr() << " 个可用写入位置\n";
        int_type rc = std::strstreambuf::overflow(c);
        std::cout << "overflow() 后:放置区的大小为 " << epptr()-pbase()
                  << ",有 " << epptr()-pptr() << " 个可用写入位置\n";
        return rc;
    }
};
 
int main()
{
    mybuf sbuf; // 读写动态 strstreambuf
    std::iostream stream(&sbuf);
 
    stream << "Sufficiently long string to overflow the initial allocation, at least "
           << " on some systems.";
}

可能的输出:

overflow() 前:放置区的大小为 16,有 0 个可用写入位置
overflow() 后:放置区的大小为 32,有 15 个可用写入位置
overflow() 前:放置区的大小为 32 个 0 个可用写入位置
overflow() 后:放置区的大小为 64 个 31 个可用写入位置
overflow() 前:放置区的大小为 64 个 0 个可用写入位置
overflow() 后:放置区的大小为 128 个 63 个可用写入位置

参阅

从放置区写入字符到关联的输出序列
(std::basic_streambuf<CharT,Traits> 的虚受保护成员函数) [编辑]
后附字符到输出序列
(std::basic_stringbuf<CharT,Traits,Allocator> 的虚受保护成员函数) [编辑]
从放置区写字符到关联的文件
(std::basic_filebuf<CharT,Traits> 的虚受保护成员函数) [编辑]
写一个字符到放置区域,并推进下一位置指针
(std::basic_streambuf<CharT,Traits> 的公开成员函数) [编辑]
插入字符
(std::basic_ostream<CharT,Traits> 的公开成员函数) [编辑]