Effective STL Terms 1: Choose your container carefully

zhaozj2021-02-11  178

Terms 1: Choose your container carefully

You know there are a lot of containers you can dominate in C , but do you realize how much? To make sure you have not ignored your option, here has a quick review.

Standard STL Sequence Container: Vector, String, Deque, and List. Standard STL Association Contains: SET, MULTISET, MAP, and MULTIMAP. Non-standard sequence containers SLIST and ROPE. SLIST is a one-way linked list, and ROPE is essentially a heavy string. ("Rope" is a heavy "string". Do you understand?) You can find an overview of these non-standard (but common) containers in terms 50. Non-standard related container hash_set, haveh_multiset, haveh_map, and hash_multimap. I tested these Hash-based containers and standard associated containers that can be widely obtained. Vector can be used as a String alternative. Terms 13 describes situations where this alternative may make sense. Vector as a replacement of standard related containers. As mentioned in Terms 23, sometimes VECTOR can perform better than standard containers in time and space. Several standard non-STL containers, including arrays, BitSet, Valarray, Stack, Queue, and Priority_Queue. Because they are non-STL containers, I am very small in this book, although the clause 16 mentioned a more advantageous situation than the STL container, and the terms 18 reveals why BitSet may be better than Vector . It is worth noting that the array can be combined with the STL algorithm because the pointer can be used as an iterator of the array.

This is all options and can consider the range and can be as rich as the choice between them. Not walking is that most of the STL is limited to a very narrow vision of the container world, ignoring a lot of questions about choosing appropriate containers. Even the standards have been involved in this action, providing the following guidance schemes between Vector, Deque, and List:

Vector, List and Deque are provided to the programmer's different complexity, so it should be used: Vector is a sequence type that can be used by default. When inserting and deleting in the middle of the sequence, it should be used when most insertion And delete the header or tail that occurs in the sequence can be selected. DEQUE this data structure.

If you mainly care about the algorithm complexity, I think this program is a reason suggestion, but you need to care more.

Now, we have to check some important containers related to complexity complexity, but first I need to introduce a classification method for STL containers. It is not as much as it should be. That is the difference between continuous memory containers and node-based containers.

Continuous memory containers (also known as array-based containers) saved their elements in one or more (dynamically assigned) memory blocks. If a new element is deleted or stored, other elements of the same memory block must be moved up or down to provide space for new elements or fill the space that is originally deleted. This move affects efficiency (see Terms 5 and 14) and unusual security (just like we will see). Standard continuous memory containers are Vector, String, and Deque. Non-standard ROPE is also a continuous memory container.

Node-based containers only save one element in each memory block (dynamic allocation). Insert or deletion of the container element only affects the pointer to the node, not the node's own content. So when there is something inserted or deleted, the element value does not need to move. Containers in the chain table - such as List and SLIST - are node-based, all standard associated containers are also (their typical implementation is balanced). Non-standard HASH containers use different node-based implementations, as we will see in terms 25. Using this inappropriate term, we are ready to describe some of the questions about the choice between containers. In this discussion, I broke the non-STL type container (such as an array, bitset, etc.), because this is this book about STL after all.

Do you need "Can I insert a new element in any location of the container"? If so, you need a sequence container, and the associated container cannot do. Do you care about the order in the container? If not, the Hash container is a feasible choice. Otherwise, you want to avoid using a Hash container. Do you have to use a container in standard C ? If so, you can remove the Hash container, SLIST, and ROPE. Which type iterator do you need? If you must be random access iterators, you can only limit Vector, Deque, and String, but you may also consider ROPE (more information about ROPE in Terms 50). If you need a two-way iterator, you can not use the general implementation of SLIST (see Terms 50) and the Hash container (see Terms 25). Whether or not when inserting or deleting data, is it very moving in the intention to move? If so, you must give up the continuous memory container (see Terms 5). Is the memory layout of the data in the container need to be compatible with C? If so, you can only use the vector (see Terms 16). Is it important to find a speed? If so, you should look at the Hash container (see Terms 25), sorted Vector (see Terms 23) and the standard associated container - probably this order. Do you mind if the reference count is used in the bottom of the container? If so, you have to avoid String because many String implementation uses reference counts (see Terms 13). You can't use ROPE because the authoritative Rope implementation is based on the reference count (see Terms 50). So you have to review your String, you can consider using Vector . Do you need to insert and delete transactional semantics? That is, do you need a reliable back to insert and delete? If so, you need to use a node-based container. If you need multi-element insertion (for example, in a range of ways - see Terms 5), you should choose List because List is the only standard container that provides multi-element insertion transaction semantics. Transaction semantics is very important for programmers who are interested in writing an abnormal security code. (The transaction semantics can also be implemented on a continuous memory container, but there will be a performance overhead, and the code is not so intuitive. To understand the knowledge of this, please refer to the Sutter "Exceptional C " clause 17 [8].) You Is it possible to minimize an iterator, pointer and reference? If so, you should use node-based containers because inserting and deleting on these containers do not make iterators, pointers, and references (unless they point to your deleted elements). In general, inserting and deleting on a continuous memory container will make all the iterators, pointers, and references that point to the container. You need a sequence container with the following characteristics: 1) You can use a random access iterator; 2) As long as there is no deletion, the insert only occurs on the end of the container, the pointer and reference data will not be invalid? This is a very special situation, but if you encounter this, Deque is your dream of containers. (Interestingly, when inserting only at the end of the container, Deque's iterator may also be invalid. Deque is the only standard STL container that "does not make its pointer and reference failure when it is the iterator.)

These problems are hard to end. For example, they do not pay attention to different container types using different memory configuration policies (Terms 10 and 14 discuss some aspects of these policies). However, they are enough for you to convince, unless you have a standard consistency, standard consistency, iterator capability, memory layout and compatibility, finding speed, because reference count is irregular, transactional semantics It is not interested in achieving the failure of the iterator, you have to spend more considerations on the algorithm complexity of the container operation. Of course, this complexity is important, but this is far from the whole story. When faced with the container, STL gave you a lot of options. If your sight exceeds the range of STL, there will be more options. Before selecting a container, ensure that all your options are considered. A "default container"? I do not think so.

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

New Post(0)