C++
迭代器 | Iterator

std::istreambuf_iterator

科技促进发展:司瑞安布夫[医]迭代器

Defined in header
template< class CharT, class Traits = std::char_traits<CharT> > class istreambuf_iterator : public std::iterator< std::input_iterator_tag, CharT, typename Traits::off_type, /* unspecified, usually CharT* */, CharT >(until C++17)
template< class CharT, class Traits = std::char_traits<CharT> > class istreambuf_iterator;(since C++17)

std::istreambuf_iterator是一个单程输入迭代器,它从std::basic_streambuf对象,它是为其构造的。

默认构造std::istreambuf_iterator被称为流尾迭代器。当一个有效的std::istreambuf_iterator到达底层流的末尾,它就等于流结束迭代器.。解除引用或增量将进一步调用未定义的行为。

std::istreambuf_iterator有一个简单的复制构造函数、一个默认构造函数和一个简单的析构函数。

成员类型

Member typeDefinition
char_typeCharT
traits_typeTraits
int_typetypename traits::int_type
streambuf_typestd::basic_streambuf<CharT, Traits>
istream_typestd::basic_istream<CharT, Traits>

成员函数

(constructor)constructs a new istreambuf_iterator (public member function)
(destructor) (implicitly declared)destructs an istreambuf_iterator (public member function)
operator*operator-> (since C++11)(until C++17)obtains a copy of the current characteraccesses a member of the current character, if CharT has members (public member function)
operator++operator++(int)advances the iterator (public member function)
equaltests if both istreambuf_iterators are end-of-stream or if both are valid (public member function)

非会员职能

operator==operator!=compares two istreambuf_iterators (function template)

成员类型

Member typeDefinition
value_typeCharT
difference_typeTraits::off_type
pointer/* unspecified, usually CharT* */
referenceCharT
iterator_categorystd::input_iterator_tag

注意:在C++17之前,这些成员类型必须通过从std::iterator<std::input_iterator_tag,CharT,Traits::off_type,/* unspecified, usually CharT* */,CharT>...

二次

#include <vector> #include <sstream> #include <iostream> #include <iterator> int main() { // typical use case: an input stream represented as a pair of iterators std::istringstream in("Hello, world" std::vector<char> v( (std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>() std::cout << "v has " << v.size() << " bytes. "; v.push_back('\0' std::cout << "it holds \"" << &v[0] << "\"\n"; // demonstration of the single-pass nature std::istringstream s("abc" std::istreambuf_iterator<char> i1(s), i2(s std::cout << "i1 returns " << *i1 << '\n' << "i2 returns " << *i2 << '\n'; ++i1; std::cout << "after incrementing i1, but not i2\n" << "i1 returns " << *i1 << '\n' << "i2 returns " << *i2 << '\n'; ++i2; // this makes the apparent value of *i2 to jump from 'a' to 'c' std::cout << "after incrementing i2, but not i1\n" << "i1 returns " << *i1 << '\n' << "i2 returns " << *i2 << '\n'; }

二次

产出:

二次

v has 12 bytes. it holds "Hello, world" i1 returns a i2 returns a after incrementing i1, but not i2 i1 returns b i2 returns a after incrementing i2, but not i1 i1 returns b i2 returns c

二次

另见

ostreambuf_iteratoroutput iterator that writes to std::basic_streambuf (class template)
istream_iteratorinput iterator that reads from std::basic_istream (class template)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppremience.com/w/cpp/iterator/istreambuf[医]迭代器