public member function
<sstream>

std::istringstream::str

string str() const;void str (const string& s);
獲取/設定內容
第一個形式(1)返回一個string物件,其中包含流的當前內容的副本。

第二種形式(2)str設定為流的內容,丟棄任何先前的內容。物件保留其開啟模式:如果其中包含ios_base::ate,則寫入位置將移至新序列的末尾。

該函式在內部呼叫其內部字串緩衝區物件的str成員。

引數

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

返回值

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

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// istringstream::str
#include <string>       // std::string
#include <iostream>     // std::cout
#include <sstream>      // std::istringstream

int main () {
  std::istringstream iss;
  std::string strvalues = "32 240 2 1450";

  iss.str (strvalues);

  for (int n=0; n<4; n++)
  {
    int val;
    iss >> val;
    std::cout << val << '\n';
  }
  std::cout << "Finished writing the numbers in: ";
  std::cout << iss.str() << '\n';
  return 0;
}

32
240
2
1450
Finished writing the numbers in: 32 240 2 1450


資料競爭

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

異常安全

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

另見