2016-11-18 18 views
-4

regex.hを使用せずにこの文字列から数値4648,4649,4650を3つのint変数に解析するにはどうすればよいですか?数字を含む文字列を整数に変換する

* SEARCH 4648 4649 4650 
a3 OK SEARCH completed 
+0

が、これは単に尋ねませんでしたか? – NathanOliver

+3

[C++で文字列をintに解析する方法は?](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

数字の数がわからない場合は、 'while(input >> value)のような' std :: vector'とwhileループを使用してください。 ){number_vector.push_back(value);} '。 –

+0

すべてのコードを追加できますか? –

関連する問題