##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::ostream_iterator

From cppreference.com
< cpp‎ | iterator

Defined in header `<iterator>` |  |
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> |  |

`std::ostream_iterator` is a single-pass `OutputIterator` 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.

### [edit] Member types

Member type | Definition
`char_type` | `CharT`
`traits_type` | `Traits`
`ostream_type` | std::basic_ostream<CharT, Traits>

### [edit] Member functions

(constructor) | constructs a new ostream_iterator (public member function) [edit]
(destructor) | destructs an `ostream_iterator` (public member function) [edit]
operator= | writes a object to the associated output sequence (public member function) [edit]
operator* | no-op (public member function) [edit]
operator++operator++(int) | no-op (public member function) [edit]

## Inherited from std::iterator

### Member types

Member type | Definition
`value_type` | void
`difference_type` | void
`pointer` | void
`reference` | void
`iterator_category` | std::output_iterator_tag

### [edit] Example

Run this code

    #include <iostream>
    #include <sstream>
    #include <iterator>
    #include <numeric>

    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

### [edit] See also

ostreambuf_iterator | output iterator that writes to std::basic_streambuf (class template) [edit]
istream_iterator | input iterator that reads from std::basic_istream (class template) [edit]

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/iterator/ostream_iterator&oldid=72454"