C++ STL ostream_iterator::operator= assignment program example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Using the C++ assignment operator to implement the output_iterator expression such as *i = x for writing to an output stream in C+= programming

To show: How to use the C++ ostream_iterator, operator=, assignment operator to implement the output_iterator expression such as *i = x for writing to an output stream in C+= programming

 

// C++ STL ostream_iterator, operator=, assignment

#include <iterator>

#include <vector>

#include <iostream>

using namespace std;

 

int main(void)

{

// 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 = 12;

*intOut = 21;

 

// no effect on iterator position

intOut++;

*intOut = 9;

*intOut = 7;

return 0;

}

 

Output examples:

 

Elements written to output stream:

12

21

9

7

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index