2010-08-24 162 views
0

我有一個看起來像這樣的的iostream - 讀字符串嵌入空白

123標籤現在是前來援助

總是有一個數字和一些標籤的時候了所有好男人的記錄文件接着是一連串的單詞。我想提取數字作爲整數,標記爲字符串,和句子作爲字符串。我已經使用getline和scan加上了一些子字符串的愚蠢行爲。

有沒有辦法做到這一點阿拉...

ispringstream iss ("123 Tag Now is the time for all good men to come to the"); 
integer i; 
string tag, sentence; 
iss >> i >> tag >> ws >> ???? >> sentence; 

即如果有某種方法可以關閉作爲終結者的白色空間,那將會很好。

+0

是在它自己的行中的每個條目或多個條目可以在同一條線? – 2010-08-24 13:52:20

+0

每行一個.... – 2010-08-24 16:32:43

回答

1

你應該能夠做到這一點在兩個步驟:

istringstream iss ("123 Tag Now is the time for all good men to come to the"); 
int i; 
std::string tag, sentence; 
iss >> i >> tag >> ws; 
std::getline(iss, sentence); 
+0

嗯,這當然比掃描更好。我也通過iss.tellg()檢索位置並在句子中使用字符串.substr。我想知道是否可以用用戶定義的操縱器來完成? – 2010-08-24 17:17:38

0

如果就沒有斷行,

iss >> i >> tag; 
getline(iss, sentence);