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::str - cpprefjp C++日本語リファレンス

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

最終更新日時: 2026年08月27日 13時44分23秒 (JST)
Akira Takahashi が更新

履歴 編集

function
<sstream>

std::basic_stringbuf::str

basic_string<CharT, Traits, Allocator> str() const;                // (1) C++98
basic_string<CharT, Traits, Allocator> str() const &;              // (1) C++20

template <class SAlloc>
basic_string<CharT, Traits, SAlloc> str(const SAlloc& sa) const;   // (2) C++20

basic_string<CharT, Traits, Allocator> str() &&;                   // (3) C++20

void str(const basic_string<CharT, Traits, Allocator>& s);  // (4) C++98

template <class SAlloc>
void str(const basic_string<CharT, Traits, SAlloc>& s);     // (5) C++20

void str(const basic_string<CharT, Traits, Allocator>&& s); // (6) C++20

template<class T>
void str(const T& t);                                       // (7) C++26

概要

文字列オブジェクトを取得または設定する。

  • (1) : 文字列オブジェクトを取得する
  • (2) : 文字列オブジェクトを取得し、SAlloc型のアロケータsaによってメモリ確保する
  • (3) : 保持する文字列オブジェクトをムーブして取得する
  • (4) : 文字列オブジェクトsを設定する
  • (5) : Allocatorに変換可能なSAlloc型のアロケータによって確保されているデータをコピーして、文字列オブジェクトsを設定する
  • (6) : 与えられた文字列オブジェクトsをムーブして設定する
  • (7) : basic_string_viewに変換可能な文字列を設定する

テンプレートパラメータ制約

  • (7) : is_convertible_v<const T&, basic_string_view<CharT, Traits>>がtrueであること

効果

  • (1) : 文字列オブジェクトを取得する
  • (2) : 文字列オブジェクトを取得して、SAlloc型のアロケータsaによって確保する
  • (3) : 保持する文字列オブジェクトをムーブして取得する
  • (4) : 文字列オブジェクトsを設定する
  • (5) : Allocatorに変換可能なSAlloc型のアロケータによって確保されているデータをコピーして、文字列オブジェクトsを設定する
  • (6) : 与えられた文字列オブジェクトsをムーブして設定する
  • (7) : std::basic_string_viewに変換可能な文字列を設定する

(4)〜(7)では、設定後に入力位置と出力位置がオープンモードに従って初期化される。オープンモードにios_base::ateが含まれる場合、出力位置は文字列の末尾に設定される。

例

基本的な使い方

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

int main()
{
  // 文字列の取得
  std::stringbuf buf;
  buf.sputn("Hello, World!", 13);
  std::cout << "1: " << buf.str() << std::endl;

  // 文字列の設定
  buf.str("New content");
  std::cout << "2: " << buf.str() << std::endl;

  // 読み書き位置のリセット
  buf.str("Read/Write test");

  // 読み取り
  char ch;
  ch = buf.sbumpc();  // 'R'
  ch = buf.sbumpc();  // 'e'
  std::cout << "3: After reading 2 chars: " << buf.str() << std::endl;

  // 書き込み(読み取り位置と書き込み位置は別なので、先頭から書き込まれる)
  buf.sputc('X');
  buf.sputc('Y');
  std::cout << "4: After writing: " << buf.str() << std::endl;

  // 空文字列でクリア
  buf.str("");
  std::cout << "5: Empty? " << (buf.str().empty() ? "yes" : "no") << std::endl;
}

出力

1: Hello, World!
2: New content
3: After reading 2 chars: Read/Write test
4: After writing: XYad/Write test
5: Empty? yes

ムーブを使用する例 (C++20)

#include <iostream>
#include <sstream>
#include <string>
#include <utility>

int main() {
  std::stringbuf buf;

  // ムーブして文字列を設定
  std::string s = "Move string";
  buf.str(std::move(s));
  std::cout << buf.str() << std::endl;

  // ムーブで文字列を取得
  std::string r = std::move(buf).str();
  std::cout << r << std::endl;
}

出力

Move string
Move string

string_viewからの設定 (C++26)

#include <iostream>
#include <sstream>
#include <string_view>

int main() {
  std::stringbuf buf;

  // 文字列リテラルを設定
  buf.str("set from string literal");
  std::cout << buf.str() << std::endl;

  // string_viewを設定
  std::string_view sv = "set from string_view";
  buf.str(sv);
  std::cout << buf.str() << std::endl;
}

出力

set from string literal
set from string_view

参照

  • P0408R7 Efficient Access to basic_stringbuf's Buffer
  • P2495R3 Interfacing stringstreams with string_view
    • C++26でstd::string_viewに対応した
  • LWG Issue 1448. Concerns about basic_stringbuf::str(basic_string) postconditions
    • C++11で、出力位置を末尾に設定するかどうかの判定がios_base::inからios_base::ateへ修正された
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.