C++ STL istreambuf_iterator::istreambuf_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++ istreambuf_iterator::istreambuf_iterator to construct an istreambuf_iterator that is initialized to read characters from the input stream in C++ programming

To show: How to use the C++ istreambuf_iterator to construct an istreambuf_iterator that is initialized to read characters from the input stream in C++ programming

 

// C++ STL istreambuf_iterator, istreambuf_iterator

#include <iterator>

#include <vector>

#include <algorithm>

#include <iostream>

using namespace std;

 

int main(void)

{

istreambuf_iterator<char>::istream_type &istrm = cin;

istreambuf_iterator<char>::streambuf_type *strmbf = cin.rdbuf();

 

cout<<"Enter a line of text, then an Enter key to insert into\n"

<<"the output, (& use a ctrl-Z Enter key combination to exit):\n";

 

istreambuf_iterator<char> charReadIn(cin);

ostreambuf_iterator<char> charOut(cout);

 

// used in conjunction with replace_copy() algorithm to insert into output

// stream and replace spaces with hyphen-separators

replace_copy(charReadIn, istreambuf_iterator<char>(), charOut, ' ', '-');

return 0;

}

 

Output examples:

 

Enter a line of text, then an Enter key to insert into

the output, (& use a ctrl-Z Enter key combination to exit):

A sample line of text

A-sample-line-of-text

Another line of string

Another-line-of-string

standard input stream buffer

standard-input-stream-buffer

standard output stream buffer

standard-output-stream-buffer

cin cout read write

cin-cout-read-write

^Z

Press any key to continue . . .

 

 

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