2015-04-02 70 views
1

有沒有辦法忽略C++內聯的字符?流中的內聯忽略

例如,在this answer我讀:

istringstream foo("2000-13-30"); 

foo >> year; 
foo.ignore(); 
foo >> month; 
foo.ignore(); 
foo >> day; 

但我希望能夠做到這一切在線:

foo >> year >> ignore() >> month >> ignore() >> day; 

我認爲這是可能在C++中,但它絕對不是爲我編譯的。也許我記得另一種語言?

回答

3

foo.ignore()是一個成員函數,所以它不能用作操縱器。它也沒有正確的返回類型和參數聲明可以作爲一個使用。你可以很容易地使自己的:

std::istream& skip(std::istream& is) { 
    return (is >> std::ws).ignore(); 
} 

foo >> year >> skip >> month >> skip >> day; 
+0

+1,我打算建議相同。但要注意,在使用通常僅用於格式化輸入的語法時,它將* formatted *輸入('>> year'等)與* unformatted input *('ignore()')混合。 – Angew 2015-04-02 13:27:51