C++ STL C++ STL ostreambuf_iterator::ostreambuf_iterator 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++ ostreambuf_iterator::ostreambuf_iterator to construct an ostreambuf_iterator that is initialized to write characters to the output stream in C++ programming

To show: How to use the C++ ostreambuf_iterator, ostreambuf_iterator to construct an ostreambuf_iterator that is initialized to write characters to the output stream in C++ programming

 

// ostreambuf_iterator, ostreambuf_iterator

#include <iterator>

#include <vector>

#include <iostream>

using namespace std;

 

int main(void)

{

// ostreambuf_iterator for stream cout

ostreambuf_iterator<char> charOut(cout);

 

// assign/insert characters

*charOut = '7';

charOut ++;

*charOut = 'T';

charOut ++;

*charOut = 'W';

 

// print the result

cout<<" are characters output."<<endl;

ostreambuf_iterator<char> strOut(cout);

string str = "These characters are being written to the output stream.\n ";

copy(str.begin(), str.end(), strOut);

return 0;

}

 

Output examples:

 

7TW are characters output.

These characters are being written to the output stream.

Press any key to continue . . .

 

 

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