GeezerVison is showing cpprefjp.github.io — std::basic_stringstream - cpprefjp C++日本語リファレンス
最終更新日時:
2026年01月08日 20時45分05秒 (JST)
yoh
が更新
履歴
編集
class template
<sstream>
std:: basic_stringstream
namespace std {
template < class CharT , class Traits = char_traits < CharT > ,
class Allocator = allocator < CharT > >
class basic_stringstream : public basic_iostream < CharT , Traits > ;
using stringstream = basic_stringstream < char > ;
using wstringstream = basic_stringstream < wchar_t > ;
}
概要
std::basic_stringstreamクラスは、文字列を出力先・入力元とするストリームであり、読み取りと書き込みの両方の操作ができる。
このクラスは、内部バッファに文字列を保持し、ストリーム操作で文字列の内容を組み立てたり、文字列内容を解析したりすることができる。
メンバ関数
非メンバ関数
名前
説明
対応バージョン
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>
int main ()
{
// 読み取りと書き込みが可能なストリーム
std :: stringstream ss ;
// データを書き込む
ss << "Hello " << 123 << " World " << 45.67 ;
// 文字列として取得
std::cout << "Content: " << ss.str () << std::endl ;
// 読み取り位置をリセット
ss.seekg ( 0 );
// データを読み取る
std::string word1 , word3 ;
int num1 ;
double num2 ;
ss >> word1 >> num1 >> word3 >> num2 ;
std::cout << "Read: word1=" << word1
<< ", num1=" << num1
<< ", word3=" << word3
<< ", num2=" << num2 << std::endl ;
// 新しい内容でリセット
ss.str ( "42 3.14159 test" );
ss.clear (); // エラーフラグをクリア
int value ;
double pi ;
std::string text ;
ss >> value >> pi >> text ;
std::cout << "New data: " << value << ", " << pi << ", " << text << std::endl ;
}
出力
Content: Hello 123 World 45.67
Read: word1=Hello, num1=123, word3=World, num2=45.67
New data: 42, 3.14159, test
参照