Name

istreambuf_iterator class template — Input iterator to read characters from a streambuf

Synopsis

template<typename charT, typename traits=char_traits<charT> >
class istreambuf_iterator :
  public iterator<input_iterator_tag, charT, typename traits::off_type, charT*,
                  charT&>
{
public:
  typedef charT char_type;
  typedef traits traits_type;
  typedef typename traits::int_type int_type;
  typedef basic_streambuf<charT,traits> streambuf_type;
  typedef basic_istream<charT,traits> istream_type;
  class proxy; // Exposition only

  istreambuf_iterator(  ) throw(  );
  istreambuf_iterator(istream_type& s) throw(  );
  istreambuf_iterator(streambuf_type* s) throw(  );
  istreambuf_iterator(const proxy& p) throw(  );
  charT operator*(  ) const;
  istreambuf_iterator<charT,traits>& operator++(  );
  proxy 
                  operator++(int);
  bool equal(istreambuf_iterator& b) const;
};

The istreambuf_iterator class template wraps a stream buffer object (instance of basic_streambuf) as an input iterator to read characters from the stream buffer. Example 13-21 shows how to use streambuf_iterators to copy files.

Examples

Example 13-21. Copying files using streambuf iterators
void copyfile(const char* from, const char* to)
{
  std::ifstream in(from);
  std::ofstream out(to);
   
  std::copy(std::istreambuf_iterator<char>(in),
            std::istreambuf_iterator<char>(  ),
            std::ostreambuf_iterator<char>(out));
}

The post-increment operator (++) returns a proxy object, which is an object that stands in for the istreambuf_iterator object. Its use is largely transparent, and ...

Get C++ In a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.