为什么连续 istream_iter 个对象递增迭代流?
Why do successive istream_iter objects increment the iterated stream?
为什么下面的代码会输出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
实例都在隐式迭代 "something internal" 到 stringstream
。为什么每个 istream_iterator
实例不从其 istream_type
的开头开始?
是的,constructor of istream_iterator
执行读取,i
、j
、k
使用相同的流,因此它们是交互式的。
(强调我的)
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.
为什么下面的代码会输出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
实例都在隐式迭代 "something internal" 到 stringstream
。为什么每个 istream_iterator
实例不从其 istream_type
的开头开始?
是的,constructor of istream_iterator
执行读取,i
、j
、k
使用相同的流,因此它们是交互式的。
(强调我的)
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.