名前空間
変種
操作

std::basic_stringbuf<CharT,Traits,Allocator>::underflow

提供: cppreference.com
< cpp‎ | io‎ | basic stringbuf
 
 
入出力ライブラリ
入出力マニピュレータ
Cスタイルの入出力
バッファ
(C++98で非推奨)
ストリーム
抽象
ファイル入出力
文字列入出力
配列入出力
(C++98で非推奨)
(C++98で非推奨)
(C++98で非推奨)
同期化出力
エラーカテゴリインタフェース
(C++11)
 
 
protected:
virtual int_type underflow()

バッファの get 領域から次の文字を読み込みます。

具体的には、

1) 入力シーケンスに利用可能な読み込み位置があれば (egptr() > gptr())、 Traits::to_int_type(*gptr()) を返します。
2) そうでなく、 pptr() > egptr() (overflow()egptr() を最後に変更したとき以降にストリームに文字が挿入された) であれば、 egptr()pptr() に等しく変更することによって最も最近に挿入された文字を含むよう��� get 領域の終端を拡張し、 Traits::to_int_type(*gptr()) を返します。
3) そうでなければ、 Traits::eof() を返します。

バッファ内の初期化済みの任意の文字は、コンストラクタに渡された文字列に由来するか overflow() によって追加された文字列に由来するかにかかわらず、入力シーケンスの一部であるとみなされます。

目次

[編集] 引数

(なし)

[編集] 戻り値

成功した場合は Traits::to_int_type(*gptr()) (get 領域の読み込む次の文字)、失敗した場合は Traits::eof()

[編集]

#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';
}

出力例:

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

[編集] 関連項目

[仮想]
紐付けられている入力シーケンスから get 領域に文字を読み込みます
(std::basic_streambuf<CharT,Traits>の仮想プロテクテッドメンバ関数) [edit]
[仮想]
紐付けられているファイルから読み込みます
(std::basic_filebuf<CharT,Traits>の仮想プロテクテッドメンバ関数) [edit]
[仮想]
次ポインタを進めずに入力シーケンスから文字を読み込みます
(std::strstreambufの仮想プロテクテッドメンバ関数) [edit]
シーケンスを進めずに入力シーケンスから文字をひとつ読み込みます
(std::basic_streambuf<CharT,Traits>のパブリックメンバ関数) [edit]