GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • cpprefjp - C++日本語リファレンス
  • GitHub Project

This view

  • Original page
  • New address

GeezerVison is showing cpprefjp.github.io — std::basic_stringbuf - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. sstream
  3. basic_stringbuf

最終更新日時: 2026年01月08日 20時45分05秒 (JST)
yoh が更新

履歴 編集

class template
<sstream>

std::basic_stringbuf

namespace std {
  template <class CharT, class Traits = char_traits<CharT>,
            class Allocator = allocator<CharT> >
  class basic_stringbuf : public basic_streambuf<CharT, Traits>;

  using stringbuf  = basic_stringbuf<char>;
  using wstringbuf = basic_stringbuf<wchar_t>;
}

概要

std::basic_stringbufクラスは、文字列を保持するストリームバッファであり、文字列を読み書きするストレージとして機能する。

メンバ関数

名前 説明 対応バージョン
(constructor) コンストラクタ
(destructor) デストラクタ
operator= ムーブ代入 C++11
swap 値の交換 C++11
str 文字列オブジェクトの設定・取得
view 文字列ビューオブジェクトの取得 C++20
get_allocator アロケータの取得 C++20

非メンバ関数

名前 説明 対応バージョン
swap 2つのオブジェクトを入れ替える C++11

メンバ型

名前 説明 対応バージョン
char_type テンプレート仮引数CharT
int_type Traits::int_type
pos_type Traits::pos_type
off_type Traits::off_type
traits_type テンプレート仮引数Traits
allocator_type テンプレート仮引数Allocator

例

#include <iostream>
#include <sstream>
#include <string>
#include <cstring>

int main()
{
  // basic_stringbufはストリームの内部バッファとして動作する
  std::stringbuf buf;

  // 書き込み: sputc()で1文字ずつ書き込む
  buf.sputc('H');
  buf.sputc('e');
  buf.sputc('l');
  buf.sputc('l');
  buf.sputc('o');

  // 文字列として取得
  std::cout << "Written: " << buf.str() << std::endl;

  // 新しい文字列を設定
  buf.str("World");

  // 読み取り: sbumpc()で1文字ずつ読み取る
  std::cout << "Read: ";
  while (buf.in_avail() > 0) {
    char c = buf.sbumpc();
    std::cout << c;
  }
  std::cout << std::endl;

  // sputn()で文字列を一度に書き込む
  buf.str("");  // バッファをクリア
  const char* message = "Hello, World!";
  buf.sputn(message, std::strlen(message));

  // C++20以降: view()で文字列ビューとして取得
  // std::string_view sv = buf.view();
  // std::cout << "View: " << sv << std::endl;

  std::cout << "Final: " << buf.str() << std::endl;
}

出力

Written: Hello
Read: World
Final: Hello, World!

参照

  • P0408R7 Efficient Access to basic_stringbuf’s Buffer
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.