2nd programs (7)

Source: Internet
Author: User
// Name: example2_3.cpp // alias: aesthetic version # include <iostream> # include <vector> # include <algorithm> # include <iterator> using namespace STD; void main (void) {typedef vector <int> int_vector; typedef struct <int> istream_itr; typedef ostream_iterator <int> ostream_itr; typedef struct <int_vector> back_ins_itr; // vector container int_vector num in STL; // read an integer from the standard input device, // copy (istream_itr (CIN), istream_itr (), back_ins_itr (Num) until the input is non-integer )); // sorting algorithm Sort (Num. begin (), num. end (); // output the sorting result to the standard output device copy (Num. begin (), num. end (), ostream_itr (cout, "/N "));}

Almost every line of code in this program is related to STL (except for the main and curly braces, and of course there are comments ), and it contains almost all the major components in STL (container iner, iterator, algorithm, adapter Adaptor). The only pity is that function objects (functor) are missing).

Do you still remember the basic features of a typical system mentioned at the beginning? -- Input + Processing + output. All of these functions are implemented through three-line statements in the preceding program. each row of statements corresponds to one operation. The data operations are highly abstract, and the combination of algorithms and containers is as easy as building blocks, and the coupling degree of the system is reduced to a very low. This is the great power of STL that shines with the light of generics. So concise, so clever, so amazing! It's like magic, so that you can never be confused again. How to implement it? Why are you so clear when you look at the second edition of the program, and you are in the fog again ).

Please pay attention to the title here (the masterpiece of aesthetics). In the actual environment, you do not need to be so perfect. After all, the destruction of good wishes often happens in life. Being too idealistic is not a good thing, at least I think so. As mentioned above, this program is just to show the unique charm of STL, you have to be impressed by its outstanding performance, maybe only those who are well versed in STL will come up with such a thing. If you only use STL in general, you can do the second version.

It is because this program is too "simple" that I cannot be sure whether I can understand three lines of code in this area through my explanation before you fully master STL, I will do my best.

The iterator mentioned above can locate and access any element in the container. In STL, this feature has been promoted. A cin represents a piece of data stream from the input device. In terms of concept, its access to the data stream function is similar to the iterator in the general sense, however, the CIN operation in C ++ is not like an iterator in many places, because its interface is inconsistent with that of the iterator (for example, the ++ operation cannot be performed on the CIN, you cannot perform value operations-that is, * operations ). To solve this problem, we need to introduce the concept of adapter. Istream_iterator is an adapter that encapsulates CIN to make it look like a common iterator, so that we can pass it as a real parameter to some algorithms (such as the copy algorithm here ). Because the algorithm only recognizes the iterator and does not accept cin. For the first copy function in the above program, the expanded form of the first parameter is istream_iterator. (CIN). The second parameter is expanded in the form of istream_iterator. () (If you are not clear about the syntax of typedef, refer to the relevant C ++ language books ). The result is to generate a temporary object for two iterators. The first one points to the beginning of the integer input data stream, and the last one points to "pass-the-end value ". This function is used to copy integer input data streams to the vector one by one from the beginning to the end. In this quasi-integer array, the first iterator repeats each time from the starting position, and finally reaches the position pointed to by the second iterator. Maybe you want to ask, if the copy function acts like I said, why not write it as follows?

copy(istream_iterator<int>(cin), istream_iterator<int>(), num.begin()); 

You can do this, but there is a little trouble. Do you still remember that array in the first version of the program is out of bounds? If you do this, you will encounter similar troubles. The reason is that when the copy function is "copying" data, if the number of input data exceeds the range of the vector container, the data will be copied outside the container. At this time, the container will not automatically increase the capacity, because it is simply copying, not inserting from the end. To solve this problem, another adapter, back_insert_iterator, appeared. Its function is to guide the copy algorithm to insert a data at the end of the container each time. The back_ins_itr (Num) in the program is expanded to back_insert_iterator. (Num), the effect is to generate such an overlay object.

I will finish the lecture 1/3 (it's not easy !), Fortunately, there is no difference between the second sentence and the previous version of the program. It is skipped here. As for the third sentence, ostream_itr (cout, "/N") is expanded in the form of ostream_iterator. (Cout, "/N"), the effect is to generate a stack object that processes the output data stream, its position points to the starting position of the data stream, and uses "/N" as the delimiter. The second copy function will start and end the Vector The content in "copy" to the output device, the iterator represented by the first parameter will carry forward from the start position each time, and finally reach the position pointed to by the iterator represented by the second parameter.

This is all content.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.