2nd program (7)

xiaoxiao2021-03-06  55

// name: eXample2_3.cpp

// alias: AESTHETIC VERSION

#include

#include

#include

#include

Using namespace std;

Void main (void)

{

TypedEf vector int_vector;

TypedEf istream_iterator iStream_iTR;

Typedef ostream_iterator ostream_iTR;

Typedef back_insert_iterator back_ins_itr;

// STOR VECTOR container

INT_VECTOR NUM;

// Read the integer from the standard input device,

/ / Until the input is not integrity data

Copy (istream_i (cin), iStream_i (), Back_ins_itr (NUM));

Sampling algorithm in // STL

sort (Num.begin (), Num.end ());

// output the sort result to the standard output device

Copy (Num.begin (), Num.end (), Ostream_i (Cout, "/ N"));

}

Almost every line of code is related to STL in this program (except for main and the pair of curly brackets, it also comments), and it contains almost all of the major parts in STL (container Container, Iterator Iterator, Algorithm) Algorithm, adapter adaptor, the only regret is to have less functions.

Remember the basic characteristics of a typical system mentioned in the beginning? - Input process output. All of these features, in the above program, is only implemented by three lines of statements, where each line statement corresponds to one. For the operation of the data, the combination between the algorithm and the container is as easy as the wood is as easy as the wood, and the system's coupling degree is lowered. This is the great force of STL that shines with generic light. So simple, so clever, so amazing! Just like magic, it is ever, I can't touch it again. How to achieve it? Why is you clear when you look at the second edition of the program, fall in five miles (happiness).

Please pay attention to the title of this place (the masterpiece of aesthetics), in the actual environment, you may not do this perfect. After all, the wishes of a beautiful wish often happen during life. It is not a good thing, at least I think so. As mentioned earlier, this program is just to showcase the unique charm of STL, you have to confess it for its excellent performance, may have only a deep STL's way of life will come out. If you are just a general use of STL, you can do this.

It is really because this program is too "simple", so that I can't affirm it, before you have not fully grasped STL, I can understand the three lines of code in this area before you have not fully grasped STL. I will do my best.

The iterator mentioned earlier can locate and access any element in the container. In STL, this feature is promoted. A CIN represents a data stream from the input device. From the concept, it is similar to the access function of the data stream. It is similar to the iterator in the general sense, but CIN in C is not like an iterator, The reason is that its interface and iterator interface are inconsistent (for example: ), it is not possible to value it, the value of * operations). In order to solve this contradiction, you need to introduce the concept of the adapter. Istream_iterator is an adapter that packs CIN to make it look like an ordinary iterator so that we can use it as a real paragraph to some algorithms (such as the Copy algorithm here). Because the algorithm only recognizes iterators without accepting CIN. For the first COPY function in the above program, the first parameter is expanded in the form: iStream_iterator (cin), the second parameter is expanded in the form: iStream_iterator

() (If you are unclear about typedef's syntax, you can refer to the relevant C language book). Its effect is a temporary object that generates two iterators. The previous one points to the beginning of the input data stream, and the latter is directed to "pass-the-end value". The role of this function is to "copy" to the head from the head to the end of the head to the vector.

This quasi-integer array, the first iterator is accumulated from the start position, and finally reaches the position pointed to by the second iterator. Maybe you have to ask, if the behavior of the COPY function is really like what I said, why not write it as the following?

Copy (istream_iterator (cin), istream_iterator (), num.begin ());

You can do this, but there is a small trouble. Remember the array critical problem in the first edition? If you write this, you will encounter similar trouble. The reason is that when the Copy function is "copy" data, if the number of input data exceeds the range of the VECTOR container, the data will be copied to the outside of the container. At this point, the container does not automatically grow capacity, as this is just a simply copy, not from the end. In order to solve this problem, another adapter back_insert_iterator debut, its role is to boot the COPY algorithm to insert a data at the end of the container each time. After the back_ins_itr (num) in the program is: Back_Insert_Iterator

(NUM), its effect is to generate such a severner object.

I will finally finish three-third (it's not easy!), I have no difference in the second sentence and the previous version, this is slightly. As for the third sentence, OSTREAM_ITR (cout, "/ n") is in the form of ostream_iterator

(cout, "/ n"),

The effect is to generate a severifier object that processes the output data stream, where its position points to the start of the data stream, and as a division in "/ N". The second COPY function will take the vector from the beginning to the end.

The content "copy" to the output device, the iterator represented by the first parameter will be taken from the start position each time, and finally reach the position indicted by the iterator represented by the second parameter.

This is all content.

转载请注明原文地址:https://www.9cbs.com/read-110366.html

New Post(0)