ostream_iterator Class

 

Visual C++ in Visual Studio 2015

The latest version of this topic can be found at ostream_iterator Class.

The template class ostream_iterator describes an output iterator object that writes successive elements onto the output stream with the extraction operator <<.

template <class Type class CharType = char class Traits = char_traits <CharType>>  
class ostream_iterator

Parameters

Type
The type of object to be inserted into the output stream.

CharType
The type that represents the character type for the ostream_iterator. This argument is optional and the default value is char.

Traits
The type that represents the character type for the ostream_iterator. This argument is optional and the default value is char_traits< CharType>.

The ostream_iterator class must satisfy the requirements for an output iterator. Algorithms can be written directly to output streams using an ostream_iterator.

Constructors

ostream_iteratorConstructs an ostream_iterator that is initialized and delimited to write to the output stream.

Typedefs

char_typeA type that provides for the character type of the ostream_iterator.
ostream_typeA type that provides for the stream type of the ostream_iterator.
traits_typeA type that provides for the character traits type of the ostream_iterator.

Operators

operator*Dereferencing operator used to implement the output iterator expression * i = x.
operator++A nonfunctional increment operator that returns an ostream_iterator to the same object it addressed before the operation was called.
operator=Assignment operator used to implement the output iterator expression * i = x for writing to an output stream.

Header: <iterator>

Namespace: std

A type that provides for the character type of the iterator.

typedef CharType char_type;

Remarks

The type is a synonym for the template parameter CharType.

Example

// ostream_iterator_char_type.cpp  
// compile with: /EHsc  
#include <iterator>  
#include <vector>  
#include <iostream>  
  
int main( )  
{  
   using namespace std;  
  
   typedef ostream_iterator<int>::char_type CHT1;  
   typedef ostream_iterator<int>::traits_type CHTR1;  
  
   // ostream_iterator for stream cout  
   // with new line delimiter:  
    ostream_iterator<int, CHT1, CHTR1> intOut ( cout , "\n" );  
  
   // Standard iterator interface for writing  
   // elements to the output stream:  
   cout << "The integers written to the output stream\n"  
        << "by intOut are:" << endl;  
 *intOut = 10;  
 *intOut = 20;  
 *intOut = 30;  
}  
\* Output:   
The integers written to the output stream  
by intOut are:  
10  
20  
30  
*\  

Dereferencing operator used to implement the output iterator expression * ii = x.

ostream_iterator<Type, CharType, Traits>& operator*();

Return Value

A reference to the ostream_iterator.

Remarks

The requirements for an output iterator that the ostream_iterator must satisfy require only the expression * ii = t be valid and says nothing about the operator or the operator= on their own. The member operator in this implementation returns *this.

Example

// ostream_iterator_op_deref.cpp  
// compile with: /EHsc  
#include <iterator>  
#include <vector>  
#include <iostream>  
  
int main( )  
{  
   using namespace std;  
  
   // ostream_iterator for stream cout  
   // with new line delimiter  
   ostream_iterator<int> intOut ( cout , "\n" );  
  
   // Standard iterator interface for writing  
   // elements to the output stream  
   cout << "Elements written to output stream:" << endl;  
 *intOut = 10;  
   intOut++;      // No effect on iterator position  
 *intOut = 20;  
 *intOut = 30;  
}  
\* Output:   
Elements written to output stream:  
10  
20  
30  
*\  

A nonfunctional increment operator that returns an ostream_iterator to the same object it addressed before the operation was called.

ostream_iterator<Type, CharType, Traits>& operator++();

ostream_iterator<Type, CharType, Traits> operator++(int);

Return Value

A reference to the ostream_iterator.

Remarks

These member operators both return *this.

Example

// ostream_iterator_op_incr.cpp  
// compile with: /EHsc  
#include <iterator>  
#include <vector>  
#include <iostream>  
  
int main( )  
{  
   using namespace std;  
  
   // ostream_iterator for stream cout  
   // with new line delimiter  
   ostream_iterator<int> intOut ( cout , "\n" );  
  
   // standard iterator interface for writing  
   // elements to the output stream  
   cout << "Elements written to output stream:" << endl;  
 *intOut = 10;  
   intOut++;      // No effect on iterator position  
 *intOut = 20;  
 *intOut = 30;  
}  
\* Output:   
Elements written to output stream:  
10  
20  
30  
*\  

Assignment operator used to implement the output_iterator expression * i = x for writing to an output stream.

ostream_iterator<Type, CharType, Traits>& operator=(
    const Type& val);

Parameters

val
The value of the object of type Type to be inserted into the output stream.

Return Value

The operator inserts val into the output stream associated with the object, followed by the delimiter specified in the ostream_iterator constructor (if any), and then returns a reference to the ostream_iterator.

Remarks

The requirements for an output iterator that the ostream_iterator must satisfy require only the expression * ii = t be valid and says nothing about the operator or the operator= on their own. This member operator returns *this.

Example

// ostream_iterator_op_assign.cpp  
// compile with: /EHsc  
#include <iterator>  
#include <vector>  
#include <iostream>  
  
int main( )  
{  
   using namespace std;  
  
   // ostream_iterator for stream cout  
   // with new line delimiter  
   ostream_iterator<int> intOut ( cout , "\n" );  
  
   // Standard iterator interface for writing  
   // elements to the output stream  
   cout << "Elements written to output stream:" << endl;  
 *intOut = 10;  
   intOut++;      // No effect on iterator position  
 *intOut = 20;  
 *intOut = 30;  
}  
\* Output:   
Elements written to output stream:  
10  
20  
30  
*\  

Constructs an ostream_iterator that is initialized and delimited to write to the output stream.

ostream_iterator(
    ostream_type& _Ostr);

ostream_iterator(
    ostream_type& _Ostr,
    const CharType* _Delimiter);

Parameters

_Ostr
The output stream of type ostream_iterator::ostream_type to be iterated over.

_Delimiter
The delimiter that is inserted into the output stream between values.

Remarks

The first constructor initializes the output stream pointer with &_Ostr. The delimiter string pointer designates an empty string.

The second constructor initializes the output stream pointer with &_Ostr and the delimiter string pointer with _Delimiter.

Example

// ostream_iterator_ostream_iterator.cpp  
// compile with: /EHsc  
#include <iterator>  
#include <vector>  
#include <iostream>  
  
int main( )  
{  
   using namespace std;  
  
   // ostream_iterator for stream cout  
   ostream_iterator<int> intOut ( cout , "\n" );  
 *intOut = 10;  
   intOut++;  
 *intOut = 20;  
   intOut++;  
  
   int i;  
   vector<int> vec;  
   for ( i = 1 ; i < 7 ; ++i )  
   {  
      vec.push_back (  i );  
   }  
  
   // Write elements to standard output stream  
   cout << "Elements output without delimiter: ";  
   copy ( vec.begin ( ), vec.end ( ),  
          ostream_iterator<int> ( cout ) );  
   cout << endl;  
  
   // Write elements with delimiter " : " to output stream  
   cout << "Elements output with delimiter: ";  
   copy ( vec.begin ( ), vec.end ( ),  
          ostream_iterator<int> ( cout, " : " ) );  
   cout << endl;  
}  
\* Output:   
10  
20  
Elements output without delimiter: 123456  
Elements output with delimiter: 1 : 2 : 3 : 4 : 5 : 6 :   
*\  

A type that provides for the stream type of the iterator.

typedef basic_ostream<CharType, Traits> ostream_type;

Remarks

The type is a synonym for basic_ostream< CharType, Traits>, a stream class of the iostream hierarchy that defines objects that can be used for writing.

Example

See ostream_iterator for an example of how to declare and use ostream_type.

A type that provides for the character traits type of the iterator.

typedef Traits traits_type;

Remarks

The type is a synonym for the template parameter Traits.

Example

// ostream_iterator_traits_type.cpp  
// compile with: /EHsc  
#include <iterator>  
#include <vector>  
#include <iostream>  
  
int main( )  
{  
   using namespace std;  
  
   // The following not OK, but are just the default values:  
   typedef ostream_iterator<int>::char_type CHT1;  
   typedef ostream_iterator<int>::traits_type CHTR1;  
  
   // ostream_iterator for stream cout  
   // with new line delimiter:  
    ostream_iterator<int, CHT1, CHTR1> intOut ( cout , "\n" );  
  
   // Standard iterator interface for writing  
   // elements to the output stream:  
   cout << "The integers written to output stream\n"  
        << "by intOut are:" << endl;  
 *intOut = 1;  
 *intOut = 10;  
 *intOut = 100;  
}  
\* Output:   
The integers written to output stream  
by intOut are:  
1  
10  
100  
*\  

<iterator>
Thread Safety in the C++ Standard Library
Standard Template Library

Show: