C++primer container

Source: Internet
Author: User

The Access member function returns a reference, Front,back,at, and the following table, if a container is const, a const reference is returned, or a normal reference if the container is not const.

Whether the return is a reference, the key is to see if the expression in the left or right value, if the right value, return is a copy of the element, if the left value, return is a reference object.

When using auto, if you want to change the value of the variable, define it as a reference type.

This refers to pointers, references, iterators that are effective, saying that the size of the container may change.

Deleting an element's member function does not detect its deletion parameters, and you should ensure that they are present before deleting the elements.

Auto inference type occurs in the compiler, so auto does not cause the program to run inefficiently.

When auto is not used, the compiler also needs to know the type of the right operand, and then compare it to the left operand type to check whether the corresponding conversions can occur and whether implicit type conversions are required.

When using auto, if the expression has the meaning of the reference, it will remove the meaning of the reference, directly corresponding to the most primitive data type,

Auto removes the top-level const, and if Auto has a reference on it, it does not remove the effect of the const modifier.

The initial value is an array, then the pointer type is obtained using auto, and if & is taken, the inferred type is an array type.

Auto is not a real type, but a placeholder, so you cannot use some operators that are type operands, such as sizeof or typeid.

When using Forword_list, two iterator objects are used, one is the element to be manipulated, and the other is the element precursor element.

The array in the standard library does not support resize operations, and the other resize operations are based on the original container.

Shrink_to_fit is just a request, and the compiler does not necessarily guarantee that it will actually rewind the memory space.

The stack is implemented by default based on Deque, or it can be implemented on lists and vectors.

Each adapter is based on the underlying to implement its own operations, can not directly use the underlying operations, which is equivalent to the underlying operation of a limit.

Queue defaults are also implemented based on Deque.

Priority_queue is a vector-based implementation.

The standard library algorithm operates on iterators rather than containers, so the algorithm does not directly delete the container.

Callable objects in C + + have functions, function pointers, operator overloads, and the use of lambda expressions.

The form that a lambda expression has:

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

Where the capture list is a list of local variables defined in the body of the function, usually empty,

The lambda must use a tail return to specify its return type.

We can ignore the argument list and return type, but must always include the capture list and the function body

In a lambda expression, ignoring parentheses and argument lists is equivalent to defining an empty argument list, and if the return type is omitted, the return type is automatically inferred from the code in the body of the function. If the function body contains a return statement, it is inferred according to it, otherwise, the void type is returned.

A lambda expression cannot have a default argument, and the number of arguments to the lambda expression must be the same as the number of formal parameters, and once the parameters are initialized, the function body can be straight-line. Lambda is like an anonymous inline function.

The existence of a capture list allows the body of the function to access the values in the Capture list.

When a lambda is defined, the compiler generates a new unnamed class type, and when a lambda is passed to a function, a new type is defined, and an object of that type is passed with a parameter that the compiler generates an unnamed object of that class type, similar to When you use Auto to define a variable that is initialized with lambda, you define an object of the type generated from the lambda.

By default, objects generated by lambda contain data members of variables captured by the Lambda capture list, similar to normal functions, and the data members of a lambda are initialized when the lambda object is created.

The captured value is copied when the lambda is created, not when it is called.

Use BIND (in the functional header file) to extend the parameters of the function call (provided that the value of the parameter is fixed)

The BIND function is even the order in which parameters can be rescheduled

For an IO stream object, only reference types can be used.

You can return a reference to a given object through a function in the REF standard library, which is a copy of the object. There is a cref function in the standard library that generates a class that holds the Const reference, which is also located in the functional header file.

Container this piece, you need to slowly refer to the algorithm to implement

Dynamic memory Management

Never use the Get () method to return pointers to other only pointers, which can result in multiple pointers pointing to the same piece of memory space, so that any one pointer-only reference is 0, which frees the memory space, so that the memory space pointed to by the other pointer is freed, This results in an empty dangling pointer.

Only one type of pointer, shared_ptr, is also defined with a template class

Criteria restrictions for smart pointer use:

Multiple smart pointers are not initialized with the same built-in pointers

Does not delete the pointer returned by the GET ().

Not applicable get () initialization the latter is reset another smart pointer

If you use a Get () return pointer, your pointer becomes invalid when the last smart pointer is destroyed.

If the resource managed by the smart pointer is not a new allocated memory, remember to pass a delete that specifically frees the appropriate memory resources.

C++primer container

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.