Streams中的内联忽略

Inline ignore in Streams

本文关键字:Streams      更新时间:2023-10-16

有没有一种方法可以忽略C++内联中的字符?

例如,在这个答案中,我正在阅读:

istringstream foo("2000-13-30");
foo >> year;
foo.ignore();
foo >> month;
foo.ignore();
foo >> day;

但我希望能够在线完成这一切:

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

我以为这在C++中是可能的,但它绝对不是为我编译的。也许我还记得另一种语言?

foo.ignore()是一个成员函数,因此不能用作操纵器。它也没有正确的返回类型和参数声明可以作为一个声明使用。不过,你可以很容易地制作自己的:

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