C++程式:將字串轉換為整數
C++是一種靜態型別語言。編寫程式需要定義指定型別的變數。有時需要從控制檯或檔案中讀取輸入。在這種情況下,字串資料將被讀入程式。將它們轉換為其他資料型別需要特殊的運算。在本文中,我們將討論如何在C++中將字串轉換為整數。有幾種不同的技術可以做到這一點。讓我們一一探索它們。
在C++中使用stringstream
C++使用流進行不同的應用程式。這些流是檔案流、標準輸入/輸出流等。還有另一種稱為stringstream的流。它以字串作為輸入,並執行與其他流相同的操作。要使用stringstream,需要匯入sstream標頭檔案。透過插入運算子(>>)或提取運算子(<<)可以訪問流資料。
語法
#include < sstream > stringstream streamObject ( <a string input> );
語法
要使用流讀取特定型別的輸入,語法如下所示
<data type> variable; streamObject >> variable;
演算法
讓我們看看演算法來了解它是如何工作的。
- 將字串物件x作為輸入
- 建立一個stringstream物件,例如ss,並將x傳遞給該物件
- 建立一個整型變數xInt
- 使用插入運算子從ss將整數儲存到xInt中
示例
#include <iostream> #include <sstream> using namespace std; int solve( string myString) { int x; stringstream ss( myString ); ss >> x; return x; } int main() { string aNumber = "5126"; int convNumber = solve( aNumber ); cout << "The given number is: " << convNumber << endl; cout << "100 more than the given number is: " << convNumber + 100 << endl; }
輸出
The given number is: 5126 100 more than the given number is: 5226
在C++中使用sscanf()
與stringstream物件類似,一種類似的方法(也適用於C)是使用sscanf()函式。與普通的scanf()函式一樣,它接受格式字串和輸入作為字元陣列。現在從字串中,它讀取指定的值並將其插入到變數地址指向的給定變數中。檢視sscanf()函式的語法。
語法
scanf ( <a string input>, <format string>, address(s) of variable );
演算法
讓我們看看演算法來了解它是如何工作的。
- 以類似C的字元陣列格式輸入字串x
- 使用sscanf()函式,引數為x、格式字串和變數地址
- 變數值將直接從sscanf()函式儲存。
示例
#include <iostream> using namespace std; int solve( string myString) { int x; sscanf( myString.c_str(), "%d", &x ); return x; } int main() { string aNumber = "215"; int convNumber = solve( aNumber ); cout << "The given number is: " << convNumber << endl; cout << "100 more than the given number is: " << convNumber + 100 << endl; }
輸出
The given number is: 215 100 more than the given number is: 315
該程式的工作方式與之前的程式相同,但是有一點需要注意。sscanf()方法不接受類似C++的字串物件。它接受類似C的字元陣列。為此,我們使用了c_str()函式來將給定的字串引數轉換為類似C的字元陣列。
在C++中使用stoi()
將字串轉換為整數的另一個快速簡便的解決方案是使用“string”標頭檔案中的stoi()函式。此函式以字串物件作為輸入,然後將其轉換為等效的整數。
語法
#include <string> stoi ( >integer in string format> );
演算法
- 將字串物件x作為輸入
- xInt = stoi( x )
- 從給定的字串x返回xInt作為整型變數。
示例
#include <iostream> #include <string> using namespace std; int solve( string myString) { int x; x = stoi( myString ); return x; } int main() { string aNumber = "625"; int convNumber = solve( aNumber ); cout << "The given number is: " << convNumber << endl; cout << "100 more than the given number is: " << convNumber + 100 << endl; }
輸出
The given number is: 625 100 more than the given number is: 725
此函式有一些變體,例如stol()用於轉換為long,stof()用於轉換為float,等等。
在C++中使用atoi()
atoi()與stoi非常相似,但它也適用於C。它接受字元陣列格式的字串。可以透過匯入cstdlib庫來訪問它。否則,沒有太大的區別。讓我們看看語法。
語法
#include <cstdlib> atoi ( <integer in character array format> );
演算法
- 以類似C的字元陣列格式輸入字串物件x
- xInt = atoi( x )
- 從給定的字串x返回xInt作為整型變數。
示例
#include <iostream> #include <string> using namespace std; int solve( string myString) { int x; x = atoi( myString.c_str() ); return x; } int main() { string aNumber = "1024"; int convNumber = solve( aNumber ); cout << "The given number is: " << convNumber << endl; cout << "100 more than the given number is: " << convNumber + 100 << endl; }
輸出
The given number is: 1024 100 more than the given number is: 1124
結論
將字串轉換為整數有幾種不同的方法。使用stringstream和sscanf()的前兩種方法是將字串轉換為任何資料型別的通用方法,而無需更改任何處理過程,只有目標變數型別會有所不同。對於stoi()和atoi(),這些僅用於將字串轉換為整數。還有一些其他等效函式可以轉換為其他資料型別。sscanf和atoi()是基於C的函式,它們不接受字串物件。在使用它們之前,必須使用c_str()函式將字串轉換為字元陣列。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP