C + + Primer Learning summary 10th Chapter generic algorithm

Source: Internet
Author: User

10th-Generic algorithm


1. The find () generic algorithm uses an example:

2. Read-only algorithm accumulate: sums and returns the elements of the given range. Note that the first 3 parameters of accumulate determine its return type . That is, if the 3rd argument is a double, even if the iterator is int, the number of the double type is eventually returned.

3. read-only algorithm equal: compares the first two iterators to indicate whether all the elements of a range are the same as the elements of the corresponding position represented by the 3rd iterator, if the same returns true. Two container types can be different , Save the element type can also be different , as long as the elements can be compared . such as String type and const char * Comparison between types.

4. Write container algorithm fill: Assigns the specified value to the element in the container. The container cannot be empty .

5. Example of using Back_inserter (an insertion iterator ):

6. Copy and replace the algorithm. Only replace_copy does not change the value of the element within the container that the original iterator refers to.

7. Sort and unique de-reordering algorithms.

Note:unique does not really remove the container repeating element , It is only the repeating element with the value of the non-repeating element , the space behind the container is still valid, But the value is not necessarily the number.

8. Stable sorting Stable_sort and custom operation:

9. Use of Lambda and find_if:

For_earch algorithm: Invokes a unary predicate on each element in the iterator scope:

A. Lambda Features:

The BIND function: P354

13. Insert iterator Back_inserter, Front_inserter, Inserter:

For insert iterators It performs *it, ++it, it++,it--,--IT operations without any effect.

Suppose Auto It=inserter (Vc,vc.begin () +1); An insert iterator is generated, so inserting an element through it=20 is always preceded by the Vc.begin () +1:

Use Inserter:

Use Back_inserter:

Use Front_inserter:

14. Reverse Iterator P363

Reverse iterators can be obtained using the Rbegin () and Rend () methods, and their + +,--are different from those of ordinary iterators and need to be noted. A reverse iterator can be converted to a normal iterator using the base () method.


C + + Primer Learning summary 10th Chapter generic algorithm

Related Article

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.