A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cplusplus.com/reference/iterator/ostream_iterator/ below:

class template

<iterator>

std::ostream_iterator
template <class T, class charT=char, class traits=char_traits<charT> >  class ostream_iterator;

Ostream iterator


Ostream iterators are output iterators that write sequentially to an output stream (such as cout).

They are constructed from a basic_ostream object, to which they become associated, so that whenever an assignment operator (=) is used on the ostream_iterator (dereferenced or not) it inserts a new element into the stream.

Optionally, a delimiter can be specified on construction. This delimiter is written to the stream after each element is inserted.



Template parameters
T
Element type for the iterator: The type of elements inserted into the stream
charT
First template parameter of the associated basic_ostream object: The type of elements the stream handles (char for ostream).
traits
Second template parameter of the associated basic_ostream: Character traits for the elements the stream handles.
Note: The default template arguments correspond to an instantiation that uses an ostream object as associated stream.

Member types member definition in istream_iterator description ostream_type basic_ostream<charT,traits> Type of the associated output stream iterator_category output_iterator_tag Input iterator value_type void char_type charT Type of the characters handled by the associated stream traits_type traits Character traits for associated stream difference_type void pointer void reference void
Member functions
(constructor)
Construct ostream iterator (public member function)
operator*
Dereference iterator (public member function)
operator++
Increment iterator (public member function)
operator=
Assignment operator (public member function)

Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// ostream_iterator example
#include <iostream>     // std::cout
#include <iterator>     // std::ostream_iterator
#include <vector>       // std::vector
#include <algorithm>    // std::copy

int main () {
  std::vector<int> myvector;
  for (int i=1; i<10; ++i) myvector.push_back(i*10);

  std::ostream_iterator<int> out_it (std::cout,", ");
  std::copy ( myvector.begin(), myvector.end(), out_it );
  return 0;
}

Possible output:
10, 20, 30, 40, 50, 60, 70, 80, 90, 


Possible implementation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
template <class T, class charT=char, class traits=char_traits<charT> >
  class ostream_iterator :
    public iterator<output_iterator_tag, void, void, void, void>
{
  basic_ostream<charT,traits>* out_stream;
  const charT* delim;

public:
  typedef charT char_type;
  typedef traits traits_type;
  typedef basic_ostream<charT,traits> ostream_type;
  ostream_iterator(ostream_type& s) : out_stream(&s), delim(0) {}
  ostream_iterator(ostream_type& s, const charT* delimiter)
    : out_stream(&s), delim(delimiter) { }
  ostream_iterator(const ostream_iterator<T,charT,traits>& x)
    : out_stream(x.out_stream), delim(x.delim) {}
  ~ostream_iterator() {}

  ostream_iterator<T,charT,traits>& operator= (const T& value) {
    *out_stream << value;
    if (delim!=0) *out_stream << delim;
    return *this;
  }

  ostream_iterator<T,charT,traits>& operator*() { return *this; }
  ostream_iterator<T,charT,traits>& operator++() { return *this; }
  ostream_iterator<T,charT,traits>& operator++(int) { return *this; }
};

See also
istream_iterator
Istream iterator (class template)
output_iterator_tag
Output iterator category (class)
ostream
Output Stream (class)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4