C++ STL istream_iterator::istream_iterator code 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++ istream_iterator::istream_iterator to construct either an end-of-stream iterator as the default istream_iterator or a istream_iterator initialized to the iterator's stream type from which it reads in C++ programming
To show: How to use the C++ istream_iterator to construct either an end-of-stream iterator as the default istream_iterator or a istream_iterator initialized to the iterator's stream type from which it reads in C++ programming
// C++ STL istream_iterator, istream_iterator
#include <iterator>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int main(void)
{
// used in conjunction with copy() algorithm to put elements into a vector read from cin
vector<int> vec(5);
vector<int>::iterator Iter;
cout<<"Enter 5 integers separated by spaces & then a character e.g: '4 6 2 7 11 R': ";
istream_iterator<int> intvecread(cin);
// default constructor will test equal to end of stream for delimiting source range of vector
copy(intvecread, istream_iterator<int>(), vec.begin());
cin.clear();
cout<<"vec vector data: ";
for(Iter = vec.begin(); Iter != vec.end(); Iter++)
cout<<*Iter<<" ";
cout<<endl;
return 0;
}
Output examples:
Enter 5 integers separated by spaces & then a character e.g: '4 6 2 7 11 R': 1 2 3 4 5 Y
vec vector data: 1 2 3 4 5
Press any key to continue . . .