2016-11-18 87 views
-4

如何在不使用regex.h的情況下將數字4648,4649,4650從此字符串解析爲三個int變量?將包含數字的字符串解析爲整數

* SEARCH 4648 4649 4650 
a3 OK SEARCH completed 
+0

是不是這個剛纔問? – NathanOliver

+3

可能的重複[如何解析一個字符串到一個int在C + +?](http://stackoverflow.com/questions/194465/how-to-parse-a-string-to-an-int-in-c) –

回答

0

嘗試使用std::istringstream

static const std:string test_string = "* SEARCH 4648 4649 4650"; 
char asterisk; 
std::string label; 
unsigned int value1, value2, value3; 
std::istringstream input(test_string); 
input >> asterisk >> label >> value1 >> value2; 

編輯1:
對於輸入查詢多個號碼:

input >> asterisk >> label; 
std::vector<unsigned int> numbers; 
unsigned int value; 
while (input >> value) 
{ 
    numbers.push_back(value); 
} 
+0

我無法確定將有多少個數字。 該字符串可以像這樣「* SEARCH 454 446 456 45645 46465」 –

+0

如果你不知道有多少個數字,那麼使用'std :: vector'和一個while循環,比如'while(input >> value ){number_vector.push_back(value);}'。 –

+0

您能否添加所有代碼? –