public member function
<sstream>

std::stringbuf::str

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

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

引數

str
一個string物件,其內容被複制。

返回值

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

示例

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) stringbuf物件。
併發訪問同一物件可能導致資料競爭。

異常安全

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

另見