如何使用 std::istringstream 處理字串?


介紹

在本教程中,我們將學習 istringstream 以及如何使用它來處理字串。istringstream 是在 <sstream> 標頭檔案中定義的 string 類的物件。它用於從字串流中讀取資料。流是在輸入輸出裝置和程式之間的資料流(字元集合)。

<sstream> 標頭檔案定義了 3 個 string 類物件,如下所示:

  • istringstream

  • ostringstream

  • stringstream

它們都用於不同的操作,例如 istringstream 負責流輸入,ostringstream 負責流輸出,而 stringstream 則同時處理流的輸入和輸出。

istringstreams 類物件使用包含字元序列的緩衝區來處理流。要使用 istringstream 的屬性,請在程式碼中包含此標頭檔案。

#include <sstream>

讀取字串流中的整數(直到空格)

用於從字串流中讀取整數。

  • 建立一個整數字符串。

  • 建立一個 istringstream 物件,並將整數字符串變數作為引數傳遞。

  • 使用提取運算子 (>>) 檢索字串變數的值。

>> 運算子執行與 cin 類似的功能,並且為了將字串分割成標記,它使用空格。它在找到空格時將字串分割成片段。

示例

以下是 istringstream 的 C++ 實現。istringstream 物件處理數字字串變數,直到遇到空格,遇到空格後停止。

#include <iostream>     
#include <sstream>      

int main (){

   std::string stringVar = "1 2 3 4 5";
   std::istringstream issVar (stringVar);
   int number;
   issVar >> number;
   std:: cout << "the first number is " << number;
  
   return 0; 
}

輸出

the first number is 1

讀取整個整數流

istringstream 可以使用迴圈來處理整個字串流。迴圈可以是 for 迴圈,具體取決於所需的程式碼。

  • 建立一個整數字符串變數。

  • 建立一個 istringstream 物件。

  • 將整數字符串變數作為引數傳遞給 istringstream 物件。

  • 建立一個新變數 number。

  • 使用 while 迴圈處理字串,直到 istringstream 物件為空。

  • 使用提取運算子 (<<) 處理 istringstream 物件的 number 變數。

  • 列印結果。

示例 1

讓我們實現上述程式碼:

#include <iostream>
#include <sstream>
#include <string>
using namespace std;
 
// Main code
int main(){
   // Input number string
   string strVar = "1 2 3 4";
 
   // creating object of istringstream
   istringstream issVar(strVar);
 
   // Variable for string the values of strVar
   int number;
 
   // Conditional statement for checking all integers of the string 
   while (issVar >> number){
 
      cout << "That stream has number: "
      << number << "\n";
   }
 
   cout << "That stream has reached the end successfully"
   << "\n";
   return 0;
}

輸出

That stream has number: 1
That stream has number: 2
That stream has number: 3
That stream has number: 4
That stream has reached the end successfully

示例 2

可以使用 if-else 條件修改上述程式碼,以檢查流是否成功處理。

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

// Main code
int main(){
   // Number string
   string strVar = "1 2 3 4";
 
   // creating Object for istringstream
   istringstream issVar(strVar);
 
   // Variable for string the values
   int number;
 
   // Conditional statement to check the issVar
   while (issVar){
 
      //streaming the string using >> operator till white space
      issVar >> number;
 
      // processing till the end of the issVar
      if (issVar){
         cout << "The stream has number: "
         << number << "\n";
      } else {
         cout << "That stream has ended"
         << "\n";
      }
   }
 
   return 0;
}

輸出

The stream has number: 1
The stream has number: 2
The stream has number: 3
The stream has number: 4
That stream has ended

使用 istringstream 從浮點數中提取整數

在上面的示例中,我們從字串中提取整數。在這裡,我們從字串中提取浮點數和整數。

  • 建立一個字串變數,並將其初始化為浮點數。

  • 建立一個 istringstream 類物件,並將字串變數傳遞給它。

  • 宣告一個 int 型別的變數用於整數。

  • 宣告一個 float 型別的變數用於浮點數。

  • 使用提取運算子 (<<) 以及 int 和 float 變數來提取字串。

示例

上述程式碼的 C++ 實現:

#include <iostream>
#include <sstream>
using std:: istringstream;
using namespace std;

int main(){
   //initializing string 
   string strVar = "45.65";

   // istringstream object containing string variable 
   std::istringstream issVar(strVar);

   //variable for the integer data type
   int number;
   
   //variable for the float data type
   double floatVar;

   issVar >> number;

   //printing the output while separating integer and float
   cout<< "Integer number is: " << number <<endl;
   issVar >> floatVar;
   cout << "Floating number is: " << floatVar;
   
   return 0;
}

輸出

Integer number is: 45
Floating number is: 0.65

使用 std::istringstream 提取混合資料型別

到目前為止,我們已經提取了相同的資料型別和空格。使用 istringstream,您可以同時提取不同型別的資料。

在這個示例中,我們使用一個包含不同資料型別(例如字串和數字)的字串。

  • 建立一個字串變數並對其進行初始化。

  • 建立一個 istringstream 物件,並將字串變數傳遞給它。

  • 為字串的每種資料型別建立一個變數。

  • 使用 << 運算子使用 istringstream 物件和變數提取字串。

  • 使用在步驟 c 中宣告的變數列印輸出。

示例

以下是 C++ 實現。

#include <iostream>
#include <sstream> 

using namespace std;

int main() {
   //string variable 
   string strVar = "Tutorials Point 34";
     
   // variables to store different values of the string
   string first;
   string second;
   string third;
	
   //istringstream object
   std:: istringstream issVar;
   issVar.str(strVar);

   //streaming string
   issVar >> first;
   cout << "First word is : " << first << endl;

   issVar >> second;
   cout << "Second word is: " << second << endl;
 	
   issVar >> third;
   cout << "Third word is: " << third << endl;
 	
   return 0;
}

輸出

First word is : Tutorials
Second word is: Point
Third word is: 34

istringstream 的優點

  • 它用於提取字串。

  • 它可以透過建立 istringstream 物件來提取多種資料型別。

  • 它可以用於確定提取是否成功。

結論

我們已經到達本教程的結尾。在本教程中,我們透過 istringstream 處理了字串。Istringstream 使用提取運算子 (>>) 及其物件來提取字串。它可以與不同的資料型別一起使用。本教程探討了 istringstream 的含義及其在不同場景中的用途。

更新於: 2023 年 10 月 3 日

2K+ 瀏覽量

啟動你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.