Istream_iterator in the definition of <iterator> header file
The method for defining Istream_iterator variables is
Istream_iterator<t> in (STRM); (where T indicates the input type of this istream_iterator, STRM is the stream that the Istream_iterator points to)
Any type that provides input operators (>>) and output operators (<<) can create Istream_iterator objects and OSTREAM_ITERATORCFQ objects that overload the two functions on their own classes:
IStream &operator >> (IStream &is, &myclass c);
and Ostream &operator << (ostream &os, const &myclass c);
1. If the strm is empty, i.e. istream<t> in (), the variable in is equivalent to the iterator of the EOF flag.
As shown in the following definition: (The following eoff is written by me intentionally, just to let everyone know that the name is OK)
Istream_iterator<t> Eoff;
It is found that _myistr is 0x000000000 in the private variable of Eoff.
Take another look at the definition: (Note: The contents of Test.txt are (EOF))
Ifstream infile ("F:\\test.txt");
istream_iterator<int> input (infile);
To view when debugging with VS2008:
We found no: The private member of the input variable _myistr is not 0. And the private member _myval is exactly 10 (that is, the first integer in the file)
Now executes the next line of code:
++input;
Then use VS2008 to see the members of the input:
After ++input, its private member _myistr changed to 0x00000000. Is the same as the Eoff variable that was previously defined.
Then run the next line and see what the output will be:
cout<< (in = = Eoff?) "EOF": "Others") <<endl;
Obviously at this time in = = Eoff was established, the result output is "EOF"
Now we know why copy (Input,eoff,back_inserter Ivec) is so used. Since the copy function is equal to the Eoff by the input first, if it does not, insert its dereference (*input) into the final Ivec
Then add 1 and then compare it with Eoff. Until the Eoff is more equal to the end. Therefore, the istream_iterator is used as a sentinel with no flow initialization.
Copy (istream_iterator<int> (CIN),istream_iterator<int> (), Back_inserter (Ivec) is the same case. Press CTRL+Z to produce EOF flag at end of ^z
The 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 inside only an integer
istream_iterator<int> input (infile);
cout<< "The" "The" <<*input<<endl;
++input;
cout<< (input = = Eoff?) "EOF": "Others") <<endl;
cout<< "*******************" <<endl;
cout<< "Please enter an integer, press Ctrl+z to end the input:" <<endl;
Vector<int> Ivec;
Copy (istream_iterator<int> (CIN),istream_iterator<int> (), Back_inserter (Ivec));
The next sentence is to output the Ivec content screen
copy (Ivec.begin (), Ivec.end (),ostream_iterator<int> (cout, "T"));
System ("pause");
}
Ps:istream_iterator and Ostream_iterator do not define a self subtraction operation, that is,--input; Is wrong.
2. Note If for the istream_iterator<int> type, if the file inside is a letter, what will be input.
Before this, please add a letter of F:\test.txt to see what the situation is: Ten sfd (EOF)
Istream_iterator<int> Eoff;
Ifstream infile ("F:\\test.txt"); Before running, please let f:\\test.txt inside only an integer
istream_iterator<int> input (infile);
cout<< "The" "The" <<*input<<endl;
++input;
After running these sentences,istream_iterator<int> input (infile) should only point to integral type, but now for the letter, what will happen.
Let's look at the members of the current input:
When input encounters a letter, _myistr also becomes 0x00000000. So, no, no.
cout<< (input = = Eoff?) "EOF": "Others") <<endl;
will also enter "EOF", that is, input = = Eoff it. The answer is certainly yes.
In fact, Cin.good () is also false at this time. You can add a line of code to try: Cout<<cin.good () <<endl;
Now we know that when you use Istream_iterator<int> (CIN) input when you only need an integral type, you can also enter a non-int letter to terminate the input.
For ostream_iterator<t> ouput (strm, "xxx"), the same is true, but now the output is the left value:
For example:
Ostream_iterator<int> output (cout, ":");
*output = 5;
*output = 6;
or write that.
Ostream_iterator<int> output (cout, ":");
*output++ = 5;
*output++ = 6;
The results are: 5:6
Output each time the solution refers to the *output after the assignment, will make themselves add 1.
That means you don't have to use the *output++ method yourself.
Anyway:
These two sentences are best used:
Copy (istream_iterator<int> (CIN),istream_iterator<int> (), Back_inserter (Ivec));
The next sentence is to output the Ivec content screen
copy (Ivec.begin (), Ivec.end (),ostream_iterator<int> (cout, "T"));
Input that sentence better is
Vector<int> Ivec (istream_iterator<int> (CIN),istream_iterator<int> ());
This sentence is more concise. If you have any questions, please advise the heroes.
Reprint please indicate the source.