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
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
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.