• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

    最終更新日時(UTC):
    が更新

    履歴 編集

    function
    <streambuf>

    std::basic_streambuf::sputc

    namespace std {
      template<class CharT, class Traits = char_traits<CharT>>
      class basic_streambuf {
      public:
        int_type sputc(char_type c);
    
        ……
      };
    }
    

    概要

    出力列に1文字書き込む。

    効果

    • 出力列が存在する場合、現在位置にcを書き込み、現在位置を1つ進める。
    • それ以外の場合、overflow()を呼ぶ。

    戻り値

    #include <iostream>
    #include <streambuf>
    
    // streambufを継承したクラス
    class dummy_buf : public std::streambuf {
    public:
        dummy_buf(char* begin, char* end) {
            // 出力列の領域を指定
            setp(begin, end);
        }
    };
    
    int main(void) {
        char space[10] = {};
        {
            dummy_buf buf{space, space};  // 空の領域を指定
            std::cout << buf.sputc('A') << std::endl;  // 書き込みは失敗
        }
        {
            dummy_buf buf{space, space + 10};
            std::cout << buf.sputc('A') << " "  // 3文字書き込む
                      << buf.sputc('B') << " "
                      << buf.sputc('C') << std::endl;
            std::cout << space << std::endl;  // 書き込んだ文字列を出力
        }
    }
    

    出力

    -1
    65 66 67
    ABC
    

    バージョン

    言語

    • C++98

    参照