public member function
<sstream>

std::basic_stringbuf::str

get (1)
basic_string<char_type,traits_type,allocator_type> str() const;
set (2)
void str (const basic_string<char_type,traits_type,allocator_type>& str);
獲取/設定內容
第一種形式(1)返回一個basic_string物件,其中包含流緩衝區的當前內容的副本。

第二種形式(2)str設定為流緩衝區的內容,並丟棄任何先前的內容。物件保留其開啟模式:如果此模式包含ios_base::ate,則輸出指標pptr)將被移到新序列的末尾。

引數

str
一個具有相同模板引數(charTtraitsAlloc)的basic_string物件,其內容被複制。
成員型別char_typetraits_typeallocator_typebasic_stringbuf的類模板引數。

返回值

對於(1),一個basic_string物件,其中包含流緩衝區中當前內容的副本。

成員型別char_typetraits_typeallocator_typebasic_stringbuf的類模板引數。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// stringbuf example
#include <string>       // std::string
#include <iostream>     // std::cout, std::ostream, std::hex
#include <sstream>      // std::stringbuf

int main ()
{
  std::stringbuf buffer;             // empty buffer

  std::ostream os (&buffer);      // associate stream buffer to stream

  // mixing output to buffer with inserting to associated stream:
  buffer.sputn ("255 in hexadecimal: ",20);
  os << std::hex << 255;

  std::cout << buffer.str();

  return 0;
}

255 in hexadecimal: ff


資料競爭

訪問(1)或修改(2) basic_stringbuf物件。
併發訪問同一物件可能導致資料競爭。

異常安全

基本保證:如果丟擲異常,物件處於有效狀態。

另見