据我所知,迭代器类的层次结构是这样的:可以在需要转发迭代器的情况下使用输入迭代器吗?
Random access -> Bi-directional -> Forward -> Input
-> Output
正确?
我一直认为有一条规则,如果一个算法需要一个特定类型的迭代器,您可以提供链上的类别迭代器,但不能关闭。所以我正在阅读this answer,其中ildjarn
建议
建议(然后稍后更正自己)使用std::ifstream
与std::istream_iterator
和std::search
找到文件中的数据。我正要发表评论,你不能这样做,因为search
期望转发迭代器,而istream_iterator
是一个输入迭代器。但只是为了确保,我试过了:
std::istringstream iss("Elephant hats for sale.");
std::istream_iterator<char> begin(iss), end;
std::string sub("hat");
auto i = std::search(begin, end, sub.begin(), sub.end());
我没想到它会编译,但它确实。但是,结果似乎是无用的,因为如果我遵循它:
while(i != end)
{
std::cout << *i;
++i;
}
没有输出。所以,我的问题是这样的:我的编译器是否允许我使用istream_iterator
拨打search
?还是没有规则阻止这种事情?
因为我明显的错误; - ]我更新了我的答案,以链接到包装(来自Boost.Spirit)的输入迭代器,使它们适合用作前向迭代器。 – ildjarn