STL is a three-in-one good thing for containers, iterators, and algorithms. programs written using STL seem very simple. For example, input several strings from cin, each of which occupies a row, and then sort these strings in lexicographically and output them to the cout. The related code is as follows:
/* Use of the iterator
* Created: ume
* Date: 2011-12-17
*
*/
# Include <iostream>
# Include <vector>
# Include <string>
# Include <iterator>
# Include <algorithm>
Using namespace std;
Int main ()
{
Vector <string> v;
String temp;
While (getline (cin, temp ))
{
V. push_back (temp );
}
Sort (v. begin (), v. end ());
Copy (v. begin (), v. end (), ostream_iterator <string> (cout ,""));
System ("pause ");
Return 0;
}
The header file is very:
Cin/cout and getline are used in the main function. Therefore, iostream must be included;
The main function uses vector <string>, so it must contain vector and string;
Ostream_iterator is used in the main function, so it must contain iterator;
The main function also calls the sort and copy functions, so it must contain algorithm.
Except for iostream, all other header files are STL header files. Vector and string are containers, iterator is the iterator, and algorithm is the algorithm.
Compile and run this program. The result is as follows:
You
Are
The
Apple
Of
My
Eye
^ Z
Apple are eye my of the you please press any key to continue...
If you want to reverse the sorting, you only need. begin (), v. end () to sort (v. begin (), sort. end (), greater <string>.
Now we have a slightly higher requirement. Didn't the above Program Package cout into the iterator? Can we also package cin into the iterator? Of course, we can define a class to implement:
/* Line_iterator.h
* Created: ume
* Date: 2011-12-17
*
*/
Class line_iterator
{
Istream * in;
String line;
Bool is_valid;
Void read ()
{
If (* in)
Getline (* in, line );
Is_valid = (* in )? True: false;
}
Public:
Typedef input_iterator_tag iterator_category;
Typedef string value_type;
Typedef ptrdiff_t difference_type;
Typedef const string * pointer;
Typedef const string & reference;
Reference operator *()
{
Return line;
}
Pointer operator-> ()
{
Return & line;
}
Line_iterator (): in (& cin), is_valid (false)
{
}
Line_iterator (istream & s): in (& s)
{
Read ();
}
Line_iterator operator ++ ()
{
Read ();
Return * this;
}
Line_iterator operator ++ (int)
{
Line_iterator temp = * this;
Read ();
Return temp;
}
Bool operator = (const line_iterator & rhs)
{
If (in = rhs. in & is_valid = rhs. is_valid)
Return true;
If (is_valid = false & rhs. is_valid = false)
Return true;
Return false;
}
Bool operator! = (Const line_iterator & rhs)
{
Return! (* This = rhs );
}
};
The code is long and complex. Before explaining it, let's see how it works:
/* Version 2 of the iterator
* Created: ume
* Date: 2011-12-17
*
*/
# Include <iostream>
# Include <vector>
# Include <string>
# Include <iterator>
# Include <algorithm>
Using namespace std;
# Include "line_iterator.h"
Int main ()
{
Line_iterator iter (cin );
Line_iterator end_of_file;
Vector <string> v (iter, end_of_file );
Sort (v. begin (), v. end ());
Copy (v. begin (), v. end (), ostream_iterator <string> (cout ,""));
System ("pause ");
Return 0;
}
Not surprisingly, running the new version of the program has the same result as the old one. In comparison, the new version of the program does not see the explicit input statement. Obviously, this is the magic of our custom line_iterator.
Where is the input in the new program implemented? See vector <string> v (iter, end_of_file). The iterator interval is used to construct the vector. In this constructor, there must be a while loop, it is to copy the string from iter to end_of_file one by one to vector v. Inevitably, it will call the operator ++ and operator of line_iterator! =. However, end_of_file does not seem to be related to any variables, but looking back at the non-argument constructor method of line_iterator, you will find that it is actually hooked with the standard input cin. Therefore, when determining whether the end of the input is reached, the is_valid mark will be compared.
Line_iterator defining other Members is equally indispensable. The seemingly useless typedef is actually frequently used in vector, sort, copy, and other joint work. To understand them, you need to learn more about iterator.
From ume's blog-Hope is a good thing