Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/iterator/istreambuf iterator"

From cppreference.com
< cpp‎ | iterator
m (Text replace - "{{tdcl" to "{{dcl")
m (Text replace - "/sidebar" to "/navbar")
Line 1: Line 1:
 
{{cpp/title|istreambuf_iterator}}
 
{{cpp/title|istreambuf_iterator}}
{{cpp/iterator/istreambuf_iterator/sidebar}}
+
{{cpp/iterator/istreambuf_iterator/navbar}}
 
{{ddcl list begin}}
 
{{ddcl list begin}}
 
{{ddcl list header | iterator}}
 
{{ddcl list header | iterator}}

Revision as of 12:57, 15 June 2012

 
 
Iterator library
Iterator concepts
Iterator primitives
Algorithm concepts and utilities
Indirect callable concepts
Common algorithm requirements
(C++20)
(C++20)
(C++20)
Utilities
(C++20)
Iterator adaptors
Range access
(C++11)(C++14)
(C++14)(C++14)  
(C++11)(C++14)
(C++14)(C++14)  
(C++17)(C++20)
(C++17)
(C++17)
 
 

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <iterator>
</td>

<td></td> <td></td> </tr> <tr class="t-dcl ">

<td class="t-dcl-nopad">
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 >
</td>

<td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

std::istreambuf_iterator is a single-pass input iterator that reads successive characters from the std::basic_streambuf object for which it was constructed. The actual read operation is performed when the iterator is incremented, not when it is dereferenced. The first character may be read when the iterator is constructed or when the first dereferencing is done. Otherwise, dereferencing only returns a copy of the most recently read character.

The default-constructed std::istreambuf_iterator is known as the end-of-stream iterator. When a valid std::istreambuf_iterator reaches the end of the underlying stream, it becomes equal to the end-of-stream iterator. Dereferencing or incrementing it further invokes undefined behavior.

std::istreambuf_iterator has a trivial copy constructor, a constexpr default constructor, and a trivial destructor.

Contents

Member types

Member type Definition
char_type CharT
traits_type Traits
int_type typename traits::int_type
streambuf_type std::basic_streambuf<CharT, Traits>
istream_type std::basic_istream<CharT, Traits>

Member functions

constructs a new istreambuf_iterator
(public member function)
(destructor)
(implicitly declared)
destructs an istreambuf_iterator
(public member function)
obtains a copy of the current character
accesses a member of the current character, if CharT has members
(public member function)
advances the istreambuf_iterator
(public member function)
tests if both istreambuf_iterators are end-of-stream or if both are valid
(public member function)

Non-member functions

compares two istreambuf_iterators
(function template)

Member types

Member type Definition
iterator_category std::input_iterator_tag
value_type CharT
difference_type Traits::off_type
pointer /* unspecified, usually CharT* */
reference CharT

Member types iterator_category, value_type, difference_type, pointer and reference are required to be obtained by inheriting from std::iterator<std::input_iterator_tag, CharT, Traits::off_type, /* unspecified, usually CharT* */, CharT>.

(until C++17)

Example

#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';
 
}

Output:

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

See also

Template:cpp/iterator/dcl list ostreambuf iteratorTemplate:cpp/iterator/dcl list istream iterator