Effective STL Terms 17

zhaozj2021-02-16  45

Item17 uses "swap tricks" to repair extra vacancy

Suppose now you are writing a support software for TV game programs. You have been logging with potential competitors, you save them in a vector:

Class contertant {...}; vector conterts;

When the program group selects a new competitor, it will be applying for a letter, and your Vector will quickly apply for a lot of elements. When the program producers choose the expected competitors, there are only very few part of the competitors to come to the front of the Vector. (Perhaps through Partial_Sort or Partition Reference Item 31), unwanted competitors will be deleted (typically use interval delete, refer to item5). It can reduce the size of the Vector, but to Vector space (Capacity) does not work. If Vector contains 100,000 competitors at a time, its space will remain at least 100,000, even if it only saves, um, maybe 10.

In order to avoid the space that it is no longer used, you need to reduce its space, from the maximum space used to it only needs space now. This spatial reduction is often called "Shrink to Fit" Contracted to the appropriate easy programming, but the code is - how to implement - things are not as intuitive. Let me show it, wait a minute.

This is how to repair excess vacancies from the competitor vector:

Vector (Contestants) .swap (Contestants);

Expression VECTOR Creates a temporary vector, which is a copy of Contestants: The copy constructor of the Vector is done. But the version of the vector is only available for the space required. So the temporary vector does not Contains extra vacancy. Then exchange temporary objects and Contectants data, and the temporary object contains too much space, which once the Contestants, final, temporary object destruction, and release space. Yes, contraction to the right.

The same skill is also applicable to String:

String s; ... // make s large, the Erase Most // of its characterstring (s) .swap (s); // do a "shrink-to-fit" on S

Now, the language police ask me to inform you, no one guarantees that this technique really reduces the extra space. Implement the free assignment of vector or String extra space, if it is willing, and sometimes it is really willing. For example, There is a minimum spatial value, lower than this value. It may not be reduced. It is also possible to force the space for Vector and String must be a second multiple. (According to my experience, this irregularity requires, in the implementation of String More common, not vector, for example, Item15). This method is contracted to the right does not mean "Small use space", which means "Using space to achieve it, the current container size is changed to change the STL implementation) Small, however, this is the best situation you can do. Therefore, when you think of "Shrink to the right", think about "SWAP Turk"

On the other hand, a "SWAP trick" variant can be used to empty the container and reduce its size to achieve a license. Simply exchange data with the temporary object generated by the default constructor:

Vector v; string s; ... // use v and svector (). swap (v); // clear v and minimize its CapacityString (). Swap (s); // clear s and minimize its Capacity

The last observation "SWAP trick", or is the general SWAP. Exchange the contents of the two containers, including exchange their iterators, pointers, and references. In one container, pointers, and references, after transformation An container is still valid and points to the same element.

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

New Post(0)