STL container bottom layer and Java container bottom layer

The implementation of STL's underlying data structure:
1) vector: The underlying data structure is an array, which supports fast random access.

2) List: The underlying data structure is a doubly linked list, which supports rapid additions and deletions.

3) deque: The underlying data structure is a central controller and multiple buffers, supports quick additions and deletions from the beginning to the end (not in the middle), and supports random access.

4) Stack: The bottom layer is implemented by deque or list, and the reason for not using vector is that expansion is time-consuming.

5) Queue: The bottom layer is implemented with deque or list, and the reason for not using vector is that expansion is time-consuming.

6) priority_queue: The underlying data structure generally uses vector as the underlying container and heap as the processing rule to manage the implementation of the underlying container.

7) set: The underlying data structure is a red-black tree, ordered and not repeated.

8) Multiset: The underlying data structure is a red-black tree, which is ordered and repeatable.

9) map: The underlying data structure is a red-black tree, ordered and not repeated.

10) Multimap: The underlying data structure is a red-black tree, which is ordered and repeatable.

11) hash_set: The underlying data structure is a hash table, disordered and not repeated.

12) hash_map: The underlying data structure is a hash table, which is disordered and non-repetitive.

13) Hashtable: The underlying data structure is vector.

vector

Corresponding to Java's Vector implements the List interface, using the synchronized keyword, thread safety (synchronization),
corresponding to Java's ArrayList, implements the List interface

1. Talk about the underlying (storage) mechanism of std::vector: A
vector is a dynamic array with a pointer to a continuous memory space.
When the allocated space is not enough to hold the data, (GCC is double expansion, VS13 is 1.5 times expansion) (Java vector is twice, ArrayList is 1.5 times). Copy the current value to the newly allocated memory and release the original memory.

list

Corresponding to Java's LinkList implements the List interface

2. Talk about the underlying (storage) mechanism of std::list.
The data is stored in the unit of node. The address of the node is not necessarily continuous in the memory. Each time an element is inserted or deleted, an element space is configured or released. The list comes with the sorting principle of the sorting function, and other sorting uses the algorithm in the algorithm. sort sort.

The difference between vector and list
3. When to use vector and when to use list.
Vector can store elements randomly (that is, the element addresses can be directly calculated by formulas without looking up one by one), but when inserting and deleting data in non-tail parts, the efficiency is very low, suitable for simple objects, the number of objects does not change much, and random access is frequent.

The list does not support random storage and is suitable for large objects, frequent changes in the number of objects, and frequent insertions and deletions.

and

ArrayDeque corresponding to Java implements the Deque interface (the upper layer is the Queue interface)

4. Talk about the underlying mechanism of std::deque.
First of all, it is composed of many segments, and the segments are continuous spaces.

deque and vector
5. Talk about the difference between deque and vector.
Vector is a continuous linear space with unidirectional opening, and deque is a continuous linear space with bidirectional opening. (Two-way opening means that you can insert and delete elements at both ends of the head and tail).

Adapter
6. What are the containers that do not allow traversal behavior (no iterator is provided)?
Queue beginning and end

Corresponds to the Queue interface of Java

stack head top
priority_queue head top

Corresponds to Java's PriorityQueue (heap) to implement the Queue interface

7. What is the difference between vector insertion and deletion and list?
The vector is similar to the sequence table of the data structure. Inserting and deleting data requires copying and moving the existing data. If the object stored by the vector is large or the constructor is complex, the overhead is high. If it is simple and small data, it is more efficient. Better than list.

The list corresponds to the linked list of the data structure to insert and delete data. It needs to traverse the existing data, but inserting data in the header is very efficient.

To be continued. . .

Guess you like

Origin blog.csdn.net/BOWWOB/article/details/113380933