Inline ignore in Streams

余生颓废 提交于 2019-12-13 18:27:35

问题


Is there a way to ignore characters in C++ inline?

For example in this answer I'm reading in:

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

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

But I'd like to be able to do this all inline:

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

I thought this was possible in C++, but it definitely isn't compiling for me. Perhaps I'm remembering another language?


回答1:


foo.ignore() is a member function so it can't be used as a manipulator. It also doesn't have the correct return type and parameter declaration to be usable as one. You can easily make your own though:

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

foo >> year >> skip >> month >> skip  >> day;


来源:https://stackoverflow.com/questions/29413954/inline-ignore-in-streams

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!