Namespaces
Variants
Views
Actions

std::ostream_iterator

From cppreference.com
< cpp‎ | iterator
Revision as of 01:29, 12 June 2012 by P12bot (Talk | contribs)

Template:cpp/iterator/ostream iterator/sidebar 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 T,

          class CharT = char,
          class Traits = std::char_traits<charT>>
class ostream_iterator : public std::iterator<std::output_iterator_tag,

                                              void, void, void, void>
</td>

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

std::ostream_iterator is a single-pass output iterator that writes successive objects of type T into the std::basic_ostream object for which it was constructed, using operator<<. Optional delimiter string is written to the output stream after every write operation. The write operation is performed when the iterator (whether dereferenced or not) is assigned to. Incrementing the std::ostream_iterator is a no-op.

In a typical implementation, the only data members of std::ostream_iterator are a pointer to the associated std::basic_ostream and a pointer to the first character in the delimiter string.

When writing characters, std::ostreambuf_iterator is more efficient, since it avoids the overhead of constructing and destructing the sentry object once per character.

Contents

Member types

Template:tdcl list hitemTemplate:tdcl list itemTemplate:tdcl list itemTemplate:tdcl list item

Member functions

constructs a new ostream_iterator
(public member function)
destructs an ostream_iterator
(public member function)
writes a object to the associated output sequence
(public member function)
no-op
(public member function)
no-op
(public member function)

Member types

Member type Definition
iterator_category std::output_iterator_tag
value_type void
difference_type

void

(until C++20)

std::ptrdiff_t

(since C++20)
pointer void
reference void

Member types iterator_category, value_type, difference_type, pointer and reference are required to be obtained by inheriting from std::iterator<std::output_iterator_tag, void, void, void, void>.

(until C++17)

Example

#include <iostream>
#include <sstream>
#include <iterator>
#include <algorithm>
int main()
{
    std::istringstream str("0.1 0.2 0.3 0.4");
    std::partial_sum( std::istream_iterator<double>(str),
                      std::istream_iterator<double>(),
                      std::ostream_iterator<double>(std::cout, " "));
}

Output:

0.1 0.3 0.6 1

See also

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