Why doesn't the istreambuf_iterator advance work

本小妞迷上赌 提交于 2021-01-27 18:40:11

问题


I was reading Constructing a vector with istream_iterators which is about reading a complete file contents into a vector of chars. While I want a portion of a file to be loaded in to a vector of chars.

#include <iostream>
#include <fstream>
#include <iterator>
#include <vector>
#include <algorithm>

using namespace std;

int main(int argc, char *argv[])
{
    ifstream ifs(argv[1], ios::binary);
    istreambuf_iterator<char> beginItr(ifs);
    istreambuf_iterator<char> endItr(beginItr);
    advance(endItr, 4);
    vector<char> data(beginItr, endItr);
    for_each(data.cbegin(), data.cend(), [](char ch)
    {
            cout << ch << endl;
    });
}

This doesn't work, since advance doesn't work, while the aforementioned question's accepted answer works. Why doesn't the advance work on istreambuf_iterator?

Also

  endItr++;
  endItr++;
  endItr++;
  endItr++;
  cout << distance(beginItr, endItr) << endl;

returns a 0. Please somebody explain what is going on!


回答1:


Why doesn't the advance work on istreambuf_iterator?

It works. It advances the iterator. The problem is that an istreambuf_iterator is an input iterator but not a forward iterator, meaning it is a single pass iterator: once you advance it, you can never access the previous state again. To do what you want you can simply use an old-fashioned for loop that counts to 4.




回答2:


istreambuf_iterator reads successive bytes from a basic_streambuf. It does not support positioning (basic_istream::seekg and basic_istream::tellg).

This is because it is a single-pass input iterator, not a forward iterator or a random-access iterator; it is designed to be able to work with streams that do not support positioning (e.g. pipes or TCP sockets).

You may find some of the solutions in bidirectional iterator over file/ifstream useful.




回答3:


If anyone else stumbles across this problem, and wants some alternative simple code you can try something like this instead:

        std::vector<char> buffer{4};
        std::ifstream source{argv[1], std::ios_base::binary};
        source.read(buffer.data(), buffer.size());


来源:https://stackoverflow.com/questions/12245809/why-doesnt-the-istreambuf-iterator-advance-work

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