Algorithm Design and Analysis – Commonly Used STL Containers (3)

Hits: 0

Commonly used STL containers

There are many STL containers, each container is a [class template]

The type of container

①Sequence Container
②Adapter Container
③Associative Container
Refer to
Algorithm Design and Analysis – Commonly Used STL Containers (1)
Algorithm Design and Analysis – Commonly Used STL Containers (2)

2. Adapter container

Adapter container refers to its underlying container based on other containers, that is to say, the adapter container contains another container as the underlying container, and realizes the function of the adapter container on the basis of the underlying container. In fact, the adapter container can be used as a general container in the algorithm design. use. The adapter container provided by STL is as follows:
(1) stack (stack container)
① It is a stack class template, and like the stack in the data structure, it has the characteristics of last in, first out. The default underlying container for stack containers is deque. The user can also specify other underlying containers, for example, the following statement specifies the underlying container of the myst stack as a vector

stack < string , vector < string >> myst; //The second parameter specifies the underlying container as vector

②There is only one exit in the stack container, that is, the top of the stack. Elements can be inserted (pushed) and deleted (popped) at the top of the stack, and sequential traversal is not allowed, so there is no begin()/end() and rbegin() in the stack container. /rend() is a member function for iterators.
③ The main member functions are as follows:

empty(): Determine if the stack container is empty
size(): returns the actual number of elements in the stack container
push(elem): push the element into the stack 
top(): return the top element 
of the stack pop(): pop the element out of the stack

(2) queue (queue container)
① It is a queue class template, and like the queue in the data structure, it has the characteristics of first-in, first-out. The queue container does not allow sequential traversal, and there are no member functions for iterators such as begin()/end() and rbegin()/rend().
②The main member functions are as follows:

empty(): Determine if the queue container is empty
size(): Returns the actual number of elements in the queue container
front(): Returns the head element of the queue. 
back(): Returns the back element of the queue. 
push(elem): element into the queue 
pop(): element out of the queue

(3) priority_queue (priority queue container)
① is a priority queue class template/priority queue is a storage structure with restricted access operations, elements can enter the priority queue in any order. Once the element is in the priority queue container, the dequeue operation will dequeue the highest priority element in the queue.
② Its main member functions are as follows:

empty(): Determine if the priority queue container is empty
size(): Returns the actual number of elements in the priority queue container
push(elem): element elem enters the queue 
top(): gets the head element 
of the queue pop(): element dequeues

③ The priority in the priority queue is determined by the relationship function (comparison operator) of the data elements in the queue. Users can use the default relationship function (for built-in data types, the default relationship function is that the larger the value, the higher the priority) , you can also overload relational functions written by yourself.
For example the following program:

#include<stdio.h>
#include<queue>
using namespace std;
int main(){
    priority_queue< int >qu;
    qu.push( 3 );
    qu.push( 1 );
    qu.push( 2 );
     printf ( "Queue head element: %d " ,qu.top());
     printf ( "Dequeue order:" );
     while (!qu.empty()){
         printf ( "%d " ,qu.top());
        qu.pop();
    } 
    printf("\n");
    return 0;
}

Results of the:

Head element: 3 Dequeue order: 321

You may also like...

Leave a Reply

Your email address will not be published.