public member function
<iterator>

std::ostream_iterator::ostream_iterator

initialization (1)
ostream_iterator (ostream_type& s);
ostream_iterator (ostream_type& s, const char_type* delimiter);
copy (3)
ostream_iterator (const ostream_iterator& x);
Construct ostream iterator
Constructs an ostream_iterator object:

(1) initalization constructor
Constructs an ostream iterator that is associated with stream s (the object does not own or copy the stream, only stores a reference).
If delimiter is specified, and is not a null pointer, it is inserted after every element is inserted in the stream.
(2) copy constructor
Constructs an ostream iterator, with the same ostream reference and delimiter string.

Parameters

s
A stream object, which is associated to the iterator.
Member type ostream_type is the type of such stream (defined as an alias of basic_ostream<charT,traits>, where charT and trais are the second and third class template parameters).
delimiter
C-string with a sequence of character to be inserted after every insertion.
Note that the iterator keeps a copy of the pointer passed (not the content of the C-string).
x
An iterator of the same ostream_iterator type, whose state is copied.

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, 

See also