istreambuf_iterator class template — Input iterator to read characters from a streambuf
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; classproxy
; // Exposition only istreambuf_iterator( ) throw( ); istreambuf_iterator(istream_type& s) throw( ); istreambuf_iterator(streambuf_type* s) throw( ); istreambuf_iterator(constproxy
& 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_iterator
s to copy
files.
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 you rarely need to think about it.
The definition and name of the proxy class are
implementation-defined, but the class has at least the capability to
return the input character and the underlying stream buffer. This
section assumes that the class name is
proxy
. Example 13-22 shows a
prototypical implementation of
proxy
.
Example 13-22. A trivial implementation of the proxy class
template<typename charT, typename traits=char_traits<charT> >
class istreambuf_iterator<charT, traits>::proxy
{
friend template<typename charT, typename traits>
class istreambuf_iterator<charT,traits>;
charT keep;
basic_streambuf<charT,traits>* sbuf;
proxy(charT c, basic_streambuf<charT,traits>* sbuf);
: keep(c), sbuf(sbuf) {}
public:
charT operator*( ) { return keep; }
};
In the following descriptions of the member functions of
istreambuf_iterator
, the data
member sbuf
is a pointer to the
iterator's stream buffer. The sbuf
member
serves only to keep the function descriptions clear and simple; the
class is not required to have such a member, nor is the class
required to have a member with that name.
istreambuf_iterator
( )
throw( )
Constructs the end-of-stream iterator.
istreambuf_iterator
(istream_type& s) throw(
)
, istreambuf_iterator
(streambuf_type* sb) throw(
)
, istreambuf_iterator
(const
proxy
&
p) throw( )
Constructs an istreambuf_iterator
and initializes
sbuf
to s.rdbuf( )
, sb
, or p.sbuf
. If sb == 0
, an end-of-stream iterator
is constructed.
charT
operator*
( )
const
Returns sbuf
->sgetc( )
.
istreambuf_iterator<charT,traits>&
operator++
( )
Calls sbuf
->sbumpc( )
and returns *this
.
proxy
operator++
(int)
Returns proxy
(
sbuf
->sbumpc( ), sbuf)
.
bool
equal
(istreambuf_iterator& b)
const
Returns true
if both
iterators are end-of-stream iterators or if neither iterator
is an end-of-stream iterator. The iterators do not have to use
the same stream buffer.
istream_iterator class
template, ostreambuf_iterator class
template, basic_streambuf
in <streambuf>