C++ STL istream_iterator, char_type and traits_type 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 traits_type as a type that provides for the character traits type of the istream_iterator and char_type as a type that provides for the character type of the istream_iterator in C++ programming

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

 

// C++ STL istream_iterator, char_type and traits_type

#include <iterator>

#include <vector>

#include <iostream>

using namespace std;

 

int main(void)

{

// using typedef to simplify the name

typedef istream_iterator<int>::char_type chtype;

typedef istream_iterator<int>::traits_type tratype;

 

// standard iterator interface for reading elements from the input stream

cout<<"Enter integers separated by spaces & then any character e.g.: '3 4 7 T': "<<endl;

// istream_iterator for reading int stream

istream_iterator<int> intread(cin);

// end-of-stream iterator

istream_iterator<int> EOFintread;

while(intread != EOFintread)

{

cout<<"Reading data: "<<*intread<<endl;

++intread;

}

cout<<endl;

return 0;

}

 

Output examples:

 

Enter integers separated by spaces & then any character e.g.: '3 4 7 T':

1 2 3 4 5 6 t r

Reading data: 1

Reading data: 2

Reading data: 3

Reading data: 4

Reading data: 5

Reading data: 6

Press any key to continue . . .

 

 

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