1
下面是示例代码和编译结果在Linux中使用gcc4.8.1:为什么多个输入迭代器会导致意想不到的结果?
//content of test.txt
1 2 3 4 5
int main()
{
fstream fs ("test.txt", std::fstream::in);
istream_iterator<string> is1(fs),eof1;
istream_iterator<string> is2(fs),eof2;
while(is1!=eof1){
cout<<"is1:"<<*is1++<<endl;
}
while(is2!=eof2){
cout<<"is2:"<<*is2++<<endl;
}
return 0;
}
//result unexpected
$./m
is1:1
is1:3
is1:4
is1:5
is2:2
从结果中,我们可以看到使用多个输入迭代器时,它会给意想不到的结果。 有人能告诉我为什么会发生这种情况吗?