[STL] 1. Data structure in STL (constantly updated)


Note: This blog post is mainly to record the function names of the three member functions (press-in, pop-up, and top data) of each data structure in STL, but the members such as empty.empty() and length.size() are used in STL The same name is used in all data structures of, so I won’t repeat it.


Total: comparison table

data structure Name in STL Enter Out top Note
Maximum heap priority_queue< Type > Q.push() Q.pop() Q.top() Although it is a queue, the return method of the head of the queue is not Q.front(), but Q.top(), which means "top of the heap" of the largest heap.
Doubly linked list deque< Type > Q.push_front()、Q.push_back() Q.pop_front()、Q.pop_back() Q.front()、Q.back() Since it is a doubly linked list, all operations have two types: front and back.
Non-repetitive combination set< Type > st.insert() None (only delete the same as other types of erase) None (only the same head iterator st.begin() and end iterator st.end() like other types) set is special, please refer to the third part of the blog post for explanation

One, priority_queue (maximum heap)

Reference: CSDN blogger [WhiteJunior] article understanding and use of priority_queue in C++

priority_queueLiterally translated as priority queue , as an extension of queue, priority queue is included in the header file <queue>.

1. Template parameters

The priority queue has three parameters, of which the latter two have default parameters. Its declaration form is:

priority_queue< type, container, function >

Of these three parameters, the last two have formal parameters and can be omitted, but the first one cannot.

among them:

type:数据类型;
container:实现优先队列的底层容器;
function:元素之间的比较方式;

For the container , the requirement must be a container implemented in the form of an array, such as vector and deque, but not a list.

In STL, by default (without the following two parameters), vector is used as the container, and operator <(less than the sign, the top of the line is the maximum value) is the comparison method, so when only the first parameter is used, priority The queue defaults to a maximum heap, and the top element of the heap output each time is the largest element in the heap at this time.

2. Brief description

Priority queue is a relatively important data structure. It is written with a binomial queue. It can find the maximum or minimum value in a queue with O(log n) efficiency. Which is the maximum or minimum value? Depending on the nature of the priority queue is created, i.e. the artificial setting comparison function (the third parameter function) determined.

3. Member functions

Enter Out top
Q.push() Q.pop() Q.top()

4. How to achieve the minimum heap?

Method 1: Take the opposite number of the elements to be input, such as {1, -2, 3} becomes {-1, 2, -3} so that it is stored in priority_queue, it becomes a minimum of the original array Heap, but the number becomes the opposite of the original array.

Method 2: Customize the third parameter of the template parameter as described in 1, and set the comparison function to be greater than comparison, that is, the function is the same as **opertor>**, and it becomes the smallest heap.


Two, deque (two-way queue)

dequeThe literal name is a two-way queue , which is included in the header file <deque >.

Reference: CSDN blogger [Chihiro ~] article deque usage details

1. Template parameters

deque< Type >

2. Brief description

A deque is a linear two-way queue, which can be inserted from the beginning or the end; similarly, it can be deleted from the beginning or the end. Of course, it also includes insert and delete operations at any position.

3. Member functions

Since it is a doubly linked list, all operations have two types: front and back.

Enter Out top
Q.push_front() Q.pop_front() Q.front()
Q.push_back() Q.pop_back() Q.back()

Three, set (not repeating collection)

setIn order not to repeat the collection , it is included in the header file <set>.

1. Template parameters

set< Type >

2. Brief description

setIn order to not repeat the collection , that is, if there are two identical elements that want to be stored in the set, the set will only retain one of them; and the set will automatically sort the saved elements .

3. Member functions

set rather special, and not to pushbe inserted, but rather insertas insert, since set will automatically sort , it is not necessarily the inserted element is saved in the back, so you can not take the push meaning, but to take the insert was intended. (Push has the meaning of inserting backwards, and insert means inserting backwards or to any position) .

Since the set is automatically sorted, it is difficult for us to determine what its last element is, so the set type does not have a pop, only erase is used for deletion.

Enter Out top
st.insert() None (only delete the same as other types of erase) None (only the same head iterator st.begin() and end iterator st.end() like other types)

Guess you like

Origin blog.csdn.net/qq_39642978/article/details/111562006