C++ STL istreambuf_iterator::char_type program sample

 

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::char_type as a type that provides for the character type of the ostreambuf_iterator in C++ programming

To show: How to use the C++ char_type as a type that provides for the character type of the ostreambuf_iterator in C++ programming

 

// C++ STL istreambuf_iterator, char_type

#include <iterator>

#include <vector>

#include <iostream>

#include <algorithm>

using namespace std;

 

int main(void)

{

// using typedef to simplify the name

typedef istreambuf_iterator<char>::char_type chatype;

typedef istreambuf_iterator<char>::traits_type tratype;

 

cout<<"Enter line of text, then press Return key to \n"

<<"insert into the output, & use a ctrl-Z Enter key\n"

<<"combination to exit: ";

 

// istreambuf_iterator for input stream

istreambuf_iterator<chatype> charInBuf(cin);

ostreambuf_iterator<char> charOut(cout);

 

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

// output stream and replaces spaces with hash sign

replace_copy(charInBuf, istreambuf_iterator<char>(), charOut, ' ', '#');

return 0;

}

 

Output examples:

 

Enter line of text, then press Return key to

insert into the output, & use a ctrl-Z Enter key

combination to exit: This is a line of text

This#is#a#line#of#text

Another line of text

Another#line#of#text

More string sample

More#string#sample

^Z

Press any key to continue . . .

 

 

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