Generic algorithm of C + + standard library

Source: Internet
Author: User

The algorithm in this paper refers to the generic algorithm.

Basic points:

1) The algorithm uses iterators to operate.

2) do not rely on the container, but the container wants to use the algorithm, it must provide the interface.

3) The general algorithm will never perform container operations. Operation refers only to: Change the container size operation. However, the algorithm inside the container is not included in the generic.

4) The algorithm uses a callable object to manipulate the element. callable objects are typically functions.

1, the algorithm usually uses the default operator [<],[==],[>] and so on.

2, essentially invoking an object as a comparison of the algorithm.

3, the usual way is: predicates, operator overloads, lambda expressions.

5) The algorithm does not check the write operation: when the algorithm writes the element value, the programmer must ensure that the container has enough space to save. This error compiler does not complain.

6) Specific container algorithm, can change the size of the container.

Predicate:

1) includes a unary predicate and a two-tuple predicate. Meta-concept: The number of received parameters.

2) can be used instead of the comparison rules of the original algorithm. For example, find () uses predicates instead of the default requirement operator [= =].

3) A predicate can be: an expression , a function.

4) predicates have a strict limit on the number of parameters. When exceeded, use a lambda expression.

Lambda expression: [Capture list] (parameter list), return type {function Body}

1) Capture list: capturing lists.

1, you can get the local variables of the function where the lambda expression is located.

2, default is copy value. If you use a reference capture, you must ensure that the variable exists when the lambda executes.

    3, using the mutable keyword, the value of the original variable can still be changed when the value is used.

2) The return type must be returned with a tail

1, an example of an end return: auto func (int i), int (*) [ten] {function body}//returns an array of int. Size is 10

3) When the function body contains statements other than return:

1, return void by default, and return with a tail if you need to return a value.

2, if there is only one return statement. The corresponding type is returned based on the return value.

5) The expression can be assigned to a variable.

1, indeterminate : As a variable, or as a function pointer. Individuals tend to pointers.

2, simple use: Auto F2 = [&v1] {return ++v1;}

Generic algorithm of C + + standard library

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.