istream_iterator and ostream_iterator learning experience

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))


  
  
  1. ifstream infile("f:\\test.txt");
  2. 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:


  
  
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. #include <iterator> //istream_iterator,ostream_iterator,back_inserter
  5. #include <algorithm> //copy
  6. #include <Windows.h> //system
  7. using namespace std;
  8. int main()
  9. {
  10. istream_iterator< int> eoff;
  11. ifstream infile ( "f: \\ test.txt" ) ; // Before running, please let f: \\ test.txt there is only one integer
  12. istream_iterator< int> input(infile);
  13. cout<< "the first element:"<<*input<< endl;
  14. ++input;
  15. cout<<(input == eoff ? "EOF" : "others")<< endl;
  16. cout<< "*******************"<< endl;
  17. COUT << "Please enter an integer, press ctrl + z End Input:" << endl ;
  18. vector< int> ivec;
  19. copy(istream_iterator< int>( cin),istream_iterator< int>(),back_inserter(ivec));
  20. // next sentence is the content output screen ivec
  21. copy(ivec.begin(),ivec.end(),ostream_iterator< int>( cout, "\t"));
  22. system( "pause");
  23. }


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)


  
  
  1. istream_iterator< int> eoff;
  2. ifstream infile ( "f: \\ test.txt" ) ; // Before running, please let f: \\ test.txt there is only one integer
  3. istream_iterator< int> input(infile);
  4. cout<< "the first element:"<<*input<< endl;
  5. ++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:


  
  
  1. ostream_iterator< int> output( cout, " : ");
  2. *output = 5;
  3. *output = 6;

Or write


  
  
  1. ostream_iterator< int> output( cout, " : ");
  2. *output++ = 5;
  3. *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:


  
  
  1. copy(istream_iterator< int>( cin),istream_iterator< int>(),back_inserter(ivec));
  2. // next sentence is the content output screen ivec
  3. 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.


Published 222 original articles · won praise 48 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_44116998/article/details/104596985