The 11th chapter: Generic algorithm

Source: Internet
Author: User

1: Algorithm

Find,count: Read algorithm, header file algorithm;

Copy, Replace,replace_copy: Write algorithm, header file algorithm;

Find_first_of,accumulate: Read algorithm, header file numeric;

Fill,fill_n: Write algorithm, header file xutility;

Back_inserter: Write algorithm, header file iterator;

2: The algorithm does not change the size of the container, the truth is that the algorithm is independent of the container, and achieve universality, universal, making the design more simple

3: Understand the following program code:

 istream_iterator<int  > In_iter (CIN); //  read ints from CIN  istream_iterator<int  > EOF; //  IStream "End" iterator  //   read until end of file, storing what is read in Vec  while  (In_iter!= EOF)  //  increment advances the stream to the next value  //  dereference reads next value from the IStream  Vec.push_back (*in_i ter++); 

for istream_iterator<int> In_iter (CIN); Indicates that a stream input iterator has been constructed and that the iterator is ready through CIN, so that the input data can be fetched back if the dereference is made. For example, when using *in_iter, the first element of the stream will be entered, but note that the *in_iter is to get the data in the stream to return its value, and if there is no data in the stream at the time of the dereference, it will naturally tell the stream to let the stream prompt the customer for input.

For the *in_iter++ operation, its essence is *in_iter; in_iter++; while defining a flow iterator, a data is ready to be received, and the *in_iter prompts to receive the data, and the in_iter++ prompts to receive the data. Because there is no data when the iterator points to this stream. Validation: int c=*in_iter;in_iter++;c=*in_iter; the first prompts for input because the stream that the In_iter iterator points to has no data to enter, and the second moves the iterator to the next, and when it finds no data corresponding, it prompts the customer to enter the data While in the 3rd statement, because it is only a dereference, and here the iterator refers to the flow is data, and therefore do not need to prompt for input.

The whole process essentially means: when the iterator is defined, there is already input, but because it is a definition, and does not use the data, so there is no prompt, so the first dereference will prompt for input, the following as long as the iterator points to the next, it is necessary to fill in the value of the point, you need to prompt for input.

The *in_iter++ operation process also has to consider the priority issue, is to run the in_iter++; run *in_iter; therefore, assuming that input 1 2, the first 1 revenue, and moved 2 also income, and *in_iter is the first iterator dereference, Therefore, the value can be obtained, but to the next cycle, in_iter++, because there is no data, it is necessary to prompt input, only the input, will *in_iter get 2 value, so the input is 1 2 \ n ctl+z the output will be 1 2;

1 2

Get 1

Ctl+z

2--get 2

Obvious statements:

istream_iterator<int> itin (CIN); Istream_iterator<int> itin1 (CIN); Istream_iterator<int> eof;      while (itin!=EOF) {ivec.push_back (*itin); Itin+ +;}

You can see that the input is 1 2 3 o'clock, just get 1 3, because 2 has been acquired by Itin1.

4: For list objects, the list container-specific version of the member should be preferred instead of the generic algorithm.

The 11th chapter: Generic algorithm

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.