为什么连续的 istream_iter 对象会增加迭代流? [英] Why do successive istream_iter objects increment the iterated stream?
问题描述
为什么下面的代码输出c
?
Why does the following code output c
?
// main.cpp
#include <iostream>
#include <sstream>
#include <iterator>
int main( int argc, char* argv[] )
{
std::string p( "abcdefghijklmnopqrstuvwxyz" );
std::stringstream ss(p);
std::istream_iterator<char> i( ss );
std::istream_iterator<char> j( ss );
std::istream_iterator<char> k( ss );
std::cout << *k << std::endl;
return 0;
}
.
$ g++ --version
g++ (GCC) 9.2.1 20190827 (Red Hat 9.2.1-1)
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ g++ -g ./main.cpp && ./a.out
c
这有点像每个连续的 istream_iterator
实例都隐式地迭代 stringstream
的内部内容".为什么每个 istream_iterator
实例不从其 istream_type
的开头开始?
It's sort of like each successive istream_iterator
instance is implicitly iterating "something internal" to the stringstream
. Why doesn't each istream_iterator
instance start at the start of its istream_type
?
推荐答案
是的,istream_iterator
的构造函数执行读取,并且 i
、j
、k
使用相同的流所以它们是互动的.
Yes, the constructor of istream_iterator
performs reading, and i
, j
, k
use the same stream so they're interactive.
(强调我的)
3) 初始化迭代器,将流的地址存储在一个数据中成员,并且 从输入流执行第一次读取到初始化缓存值数据成员.
3) Initializes the iterator, stores the address of stream in a data member, and performs the first read from the input stream to initialize the cached value data member.
这篇关于为什么连续的 istream_iter 对象会增加迭代流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!