Previously only knew IteratorIterator class. Most of the usual programming is to take him through some kind of container.
Today, we have a new way of using it and record it.
For example, there is a task that reads a string of string elements from a standard input device, saves them in a vector, arranges them, and then writes them back to the standard output device, which is usually the case with large code. (already commented out)
#include <iostream>#include<iterator>#include<algorithm>#include<vector>#include<string>using namespacestd;intMain () {stringWord; /*vector<string>text; while (Cin>>word) {text.push_back (word); } sort (Text.begin (), Text.end ()); for (int ix = 0; IX < text.size (); ix++) cout << Text[ix] << ";*/Istream_iterator<string> is(CIN);//To bind is to a standard input deviceistream_iterator<string> EOF;//represents the next position of the last element to be read, and for a standard input device, the end-of-file represents the previous, as long as the IStream object is not assigned to the istream_iterator when it is defined, it represents end-of-file. vector<string>text; Copy ( is, EOF, Back_inserter (text));//Back_inserter is iterator Inserter, the so-called insertionadapter, avoids the use of the container's assignment, because using = must determine the size of a container beforehand. sort (Text.begin (), Text.end ()); Ostream_iterator<string> Os (cout," "); Copy (Text.begin (), Text.end (), OS); return 0;}
However, the iostream iterator can be implemented using the above method (which is not commented out).
If you want to enter into a file, you can add the following code
Ifstream in_file (" file name "); Ofstream out_file (" file name") ); if (!in_file | |! out_file) { "Unable to open the necessary files.\n"; return -1}2017-04-21 21:55:35
Using iostream Iterator