如何在不使用regex.h的情况下将数字4648,4649,4650从此字符串解析为三个int变量?将包含数字的字符串解析为整数
* SEARCH 4648 4649 4650
a3 OK SEARCH completed
如何在不使用regex.h的情况下将数字4648,4649,4650从此字符串解析为三个int变量?将包含数字的字符串解析为整数
* SEARCH 4648 4649 4650
a3 OK SEARCH completed
尝试使用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);
}
我无法确定将有多少个数字。 该字符串可以像这样“* SEARCH 454 446 456 45645 46465” –
如果你不知道有多少个数字,那么使用'std :: vector'和一个while循环,比如'while(input >> value ){number_vector.push_back(value);}'。 –
您能否添加所有代码? –
是不是这个刚才问? – NathanOliver
可能的重复[如何解析一个字符串到一个int在C + +?](http://stackoverflow.com/questions/194465/how-to-parse-a-string-to-an-int-in-c) –