2016-11-18 88 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

您能否添加所有代码? –