Effective STL Terms 3

zhaozj2021-02-11  199

Terms 3: Make the copy operation of the object in the container and correct

The container is accommodated, but it is not the object you give them. Also, when you get an object from the container, the object you get is not the object in the container. Instead, when you add an object to the container (such as by INSERT or PUSH_BACK, etc.), enter the container's copy of the object you specify. When you get an object from your container (such as through Front or Back), you take the copy of the object in the container. Turn it in, copy it. This is the way STL.

Once an object enters a container, the copy of it is not rare. If you are inserted or deleted from vector, string, or deque, existing container elements will move (copy) (see Terms 5 and 14). If you use any sort algorithm (see Terms 31): Next_Permutation or Previous_Permutation; Remove, Unique, or the same class (see Terms 32); Rotate or Reverse, etc., the object will move (copy). Yes, the copy object is a STL method.

You may be interested in all these copies. This is very simple, an object is copied by using its copy member function, especially its copy constructor and its copy assignment operator (very good name, isn't it?). For user-defined classes, such as widget, these functions are traditionally declared:

Class widget {

PUBLIC:

...

Widget (const widget &); // copy constructor

Widget & Operator = (const widget &); // copy assignment operator

...

}

If you don't declare these functions, your compiler will always declare them. Copying built-in type (such as ING, pointer, etc.) is also always done by simply copying their inner bits. (For details on copy constructor and assignment operators, please refer to any C introductory book. In "Effective C ", clauses 11 and 27 focus on these functions.)

Because all of these copies will occur, the motivation of this Territor is now very clear. If you use a copy process to fill a container, a simple operation - put the object into the container will also prove to be a performance bottleneck. The more things you will move in the container, you will waste more memory and clock cycles on a copy. In addition, if you have a non-traditional "copy" object, put this object to the container will always lead to misfortune. (An example of the unfortunate one can see the terms 8.)

Of course, the copy will result in segmentation due to inheritance. That is to say, if you create a container with a base class object, and you try to insert a derived class object, then the object's derived portion of the object will be deleted when the object (copy constructor through the base class) is copied into the container.

Vector VW;

Class SpecialWidget: // Specialwidget derived from the widget from above

Public widget {...};

SpecialWidget Sw;

vw.push_back (sw); // SW is copied into VW as a base class object

// When the copy is lost, its special part is lost.

The segmentation problem implies that the container that inserts a derived object into the base class object is almost always wrong. If you want the result object to be derived as a derived class object, for example, the virtual function of calling the derived class is always wrong. (For more background knowledge, please refer to "Effective C " Terms 22. It will occur in another example of occurring in STL, see Terms 38.) A simple copy of the copy, correct and correct The way is a container that establishes a pointer instead of an object. That is, it is not a Widget's container to create a container of Widget *. The copy pointer is very fast, it always strictly be your desirable (指 copy bit), and there is no division when the pointer is copied. Unfortunately, the pointer's container has their own STL-related headaches. You can understand them from terms 7 and 33. If you want to avoid these headaches and still avoid efficiency, correctness, and segmentation, you may find that the container of the smart pointer is an attractive choice, please go to Terms 7.

If all of these make STL copying mechanisms are crazy, please re-think about it. Yes, STL performs a lot of copies, but it is typically designed to avoid unnecessary object copies, in fact, it is also implemented to avoid unnecessary object copies. And C and C built-in container behavior make a comparison, the array below:

Widget W [MaxNumWidgets]; // Establish a size of the widgets for MaxNumWidgets

/ / Conformably each element is constructed by default

Even if we typically use some of them or we immediately use the value of each default constructor from a certain place (for example, a file), this also has to construct the MaxNumWidget Widget object. With STL instead of array, you can use a Vector: you can grow when you need:

Vector vw; // Create a vector for 0 Widget objects

// can be expanded when you need it

We can also create an empty vector that can contain the MaxNumWidgets Widget, but there is no constructing widget:

Vector VW;

Vw.reserve (MAXNUMWIDGETS); For more information on // reserve, see Terms 14

And array contrast, STL containers more civilized. They only build (by copy) the number of objects you need, and they only do when you are specified. Yes, we need to know that the STL container uses a copy, but don't forget a fact: it is still a progress than the array.

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

New Post(0)