Inline ignore in Streams

2019-08-07 00:36发布

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条回答
Ridiculous、
2楼-- · 2019-08-07 01:06

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;
查看更多
登录 后发表回答