我正在阅读Constructing a vector with istream_iterators这是关于将完整的文件内容读入字符向量。虽然我希望将文件的一部分加载到字符向量中。
#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;
});
}
这不起作用,因为提前不起作用,而上述问题的接受答案有效。为什么提前不工作istreambuf_iterator
?
还
endItr++;
endItr++;
endItr++;
endItr++;
cout << distance(beginItr, endItr) << endl;
返回 0。请有人解释发生了什么!