Reprinted from fdl19881
istream_iterator defined <iterator> header file
Istream_iterator way to define variables
istream_iterator <T> in (strm); (wherein T indicates istream_iterator this type of input, strm stream is directed istream_iterator)
Providing the operator input (>>) and any type of output operators (<<) can create objects and ostream_iteratorcfq istream_iterator objects, i.e. their class overrides these two functions:
istream &operator >> (istream &is, &MyClass c);
和 ostream &operator << (ostream &os , const &MyClass c);
1. If strm is empty, i.e. such istream <T> in (); while, in this case the variable is equivalent to the EOF flag is directed the iterator
Figure defined as follows: (eoff below that I deliberately wrote, just to let everyone know just from the name will do)
istream_iterator<T> eoff ;
Eoff private variables found in _Myistr is 0x000000000;
Then look at a definition: (Note: the contents of test.txt is 10 (EOF))
-
ifstream infile("f:\\test.txt");
-
istream_iterator<
int> input(infile);
Check again when vs2008 debugging:
We find no: private member _Myistr input variable is not zero up! And private members _Myval exactly (ie, the first integer file) 10
Now in the next line of code:
++input;
VS2008 and then look at the input members:
After ++ input, it becomes 0x00000000 a private members _Myistr! And eoff defined as a variable before!
And then the next line and see what output:
cout<<(in == eoff ? "EOF" : "others")<<endl;
Obviously at this time in == eoff set up, the resulting output is "EOF"
Now that we know (input, eoff, back_inserter (ivec) such usage reasons copy of it. Because the copy function is more equality through the first input and eoff, such as if it will dereference (* input) is inserted into the ivec At last
Since then plus 1, and then compared with eoff. Until the end of relatively equal eoff! Therefore, the use of unused istream_iterator stream initialized as sentinels.
copy (istream_iterator <int> (cin), istream_iterator <int> (), back_inserter (ivec) is the same case. At the end of pressing need to generate the EOF flag ctrl + z ^ z
Complete procedure is as follows:
-
#include <iostream>
-
#include <vector>
-
#include <fstream>
-
#include <iterator> //istream_iterator,ostream_iterator,back_inserter
-
#include <algorithm> //copy
-
#include <Windows.h> //system
-
using
namespace
std;
-
int main()
-
{
-
istream_iterator<
int> eoff;
-
ifstream infile ( "f: \\ test.txt" )
;
// Before running, please let f: \\ test.txt there is only one integer
-
istream_iterator<
int> input(infile);
-
cout<<
"the first element:"<<*input<<
endl;
-
++input;
-
cout<<(input == eoff ?
"EOF" :
"others")<<
endl;
-
-
cout<<
"*******************"<<
endl;
-
COUT
<<
"Please enter an integer, press ctrl + z End Input:"
<<
endl
;
-
vector<
int> ivec;
-
copy(istream_iterator<
int>(
cin),istream_iterator<
int>(),back_inserter(ivec));
-
// next sentence is the content output screen ivec
-
copy(ivec.begin(),ivec.end(),ostream_iterator<
int>(
cout,
"\t"));
-
system(
"pause");
-
}
ps: istream_iterator ostream_iterator are not defined and decrement, i.e. --input; is wrong!
2. Note that if for istream_iterator <int> type, if there is a file for the letter, then, input will happen then?
Prior to this, please f: \ test.txt plus a letter and see what that is, content: 10 sfd (EOF)
-
istream_iterator<
int> eoff;
-
ifstream infile ( "f: \\ test.txt" )
;
// Before running, please let f: \\ test.txt there is only one integer
-
istream_iterator<
int> input(infile);
-
cout<<
"the first element:"<<*input<<
endl;
-
++input;
After a few runs this, istream_iterator <int> input (infile) should only point to integer, but now for the letter, and how will it?
We look at the input of the members present:
input the face of the letter, _Myistr also become a 0x00000000. So do not say
cout<<(input == eoff ? "EOF" : "others")<<endl;
Will enter "EOF", that is, input == eoff it? The answer is yes indeed!
In fact, at this time cin.good () also is false, we can add a line of code to try: cout << cin.good () << endl;
Now we know that when only integer, use istream_iterator <int> (cin) input, you can enter a non-int type of letter to terminate input.
For ostream_iterator <T> ouput (strm, "xxx"); it is the same, but now the left output values:
E.g:
-
ostream_iterator<
int> output(
cout,
" : ");
-
*output =
5;
-
*output =
6;
Or write
-
ostream_iterator<
int> output(
cout,
" : ");
-
*output++ =
5;
-
*output++ =
6;
The results are: 5: 6
output dereference * After each assignment after output, will make their own self plus one.
That is not their own use of the method * output ++.
In short:
These two are the best to use:
-
copy(istream_iterator<
int>(
cin),istream_iterator<
int>(),back_inserter(ivec));
-
// next sentence is the content output screen ivec
-
copy(ivec.begin(),ivec.end(),ostream_iterator<
int>(
cout,
"\t"));
Enter the phrase is better
vector<int> ivec(istream_iterator<int>(cin),istream_iterator<int>());
Sentence more concise.
If the write-in question, please enlighten you heroes.Please indicate the source.