2014-02-21 514 views
-7

用戶引入了一個像「1 10 4 1 53」這樣的字符串,我必須讀取字符串中的所有數字。我如何在C++中做到這一點?如何讀取由C++中的空格分隔的字符串?

+5

這個和類似的問題已經在這裏被無數次覆蓋;你嘗試搜索嗎? – TypeIA

+1

關鍵字是「StackOverflow C++讀文件整數空格分隔」 –

回答

0

如果速度不是你所關心的,請使用stringstream

#include <string> 
#include <sstream> 
#include <iostream> 
using namespace std; 

int 
main() 
{ 
    string str("1 10 4 1 53"); 
    stringstream ss(str); 
    int n; 
    while (ss >> n) 
    cout << n << endl; 
    return 0; 
} 
0

你可以輸入一個字符串,然後你可以使用標記化向strtok()字符串分隔字符串。

+3

1980調用;他們希望他們的字符串標記化技術回來......(這是__34年前!!) –

相關問題