当前位置:网站首页>Container adapter - stack: stack queue: queue priority_ Queue: priority queue
Container adapter - stack: stack queue: queue priority_ Queue: priority queue
2022-06-28 08:42:00 【Jinky strange 18】
Adapter : send A kind of The behavior of things similar On in addition A kind of A mechanism by which things behave
Containers Adapter : No own data structure , Bottom call Other Data container structure , Pack containers , Make it behave in another way
The standard library provides three sequential container adapters :stack( Stack : First in, then out ) queue( queue : fifo ) priority_queue( Priority queue : By Priority (“<” Number ) Sort ) , Iterators are not supported
The header files required to use the container adapter are :
#include <stack> //stack
#include<queue> //queue、priority_queue
By default , Stacks and queues are based on deque Realized , The priority queue is based on vector Realized .
Stack stack It's characterized by being first in and then out , So it You can use Any one The order Containers .
queue queue It is characterized by first in, first out , You must provide pop_front operation , therefore Cannot be established on vector On the container ; Priority queue priority_queue, want Support random access , therefore Can be built on vector perhaps deque On , Cannot be established on list On .
species | characteristic | Default sequence container | Available sequential containers | Sequence containers are not available |
stack | First in, then out , You can use any kind of sequential container | deque | vector、list、deque | nothing |
queue | fifo , You must provide pop_front operation | deque | list、deque | vector |
priority_queue | To support random access | vector | vector、deque | list |
stack Operations provided :
* top(): Returns a reference to a stack top element , The type is T&. If the stack is empty , The return value is undefined .
* push(const T& obj): You can push copies of objects onto the top of the stack . This is done by calling the underlying container push_back() Function completed .
* push(T&& obj): Push objects into the top of the stack by moving them . This is done by calling the right value reference parameter of the underlying container push_back() Function completed .
* pop(): Pop up top element .
* size(): Returns the number of elements in the stack .
* empty(): Returns... When there are no elements in the stack true.
* emplace(): Call the constructor with the arguments passed in , Generate objects at the top of the stack .
* swap(stack<T> & other_stack): Exchange the elements in the current stack with the elements in the parameters . Parameter must contain the same type of element as the current stack . about stack Object has a special global function swap() have access to .
queue Provided operation
* front(): return queue Reference to the first element in . If queue Is a constant , Just return a frequent quote ; If queue It's empty , The return value is undefined .
* back(): return queue Reference to the last element in . If queue Is a constant , Just return a frequent quote ; If queue It's empty , The return value is undefined .
* push(const T& obj): stay queue Add a copy of the element at the end of . This is by calling the member functions of the underlying container push_back() To complete .
* push(T&& obj): In a mobile way queue Add elements to the tail of . This is by calling the member function of the underlying container with the right value reference parameter push_back() To complete .
* pop(): Delete queue The first element in .
* size(): return queue The number of elements in .
* empty(): If queue If there is no element in , return true.
* emplace(): Pass it on to emplace() Parameter call to T Constructor for , stay queue The tail of the generated object .
* swap(queue<T> &other_q): Will the current queue Elements and parameters in queue The exchange of elements in . They need to contain elements of the same type . You can also call the global function template swap() To do the same thing .
priority_queue Operations provided
* push(const T& obj): take obj Put a copy of the container in place , This usually involves a sort operation .
* push(T&& obj): take obj Put the container in place , This usually involves a sort operation .
* emplace(T constructor a rgs...): By calling the constructor of the passed in parameter , Construct a... At the appropriate position in the sequence T object . To maintain priorities , Usually you need a sort operation .
* top(): Returns the reference of the first element in the priority queue .
* pop(): Remove the first element .
* size(): Returns the number of elements in the queue .
* empty(): If the queue is empty , return true.
* swap(priority_queue<T>& other): Exchange with the element of the parameter , The contained objects must be of the same type .
********************************************* Summary of containers ********************************************
Standard library container class | explain |
Sequence containers |
|
vector( parameter ) | 1. Capacity expansion is multiplied , It leads to a waste of space : Suppose at the beginning 2 Space , Use 1 individual , Another waste ;pop It's a waste of space 2. It can be quickly inserted from the back 、 Delete as O(1), from The leading insert or delete is O(n) 3. Direct access to any element |
list( Bidirectional circular linked list ) | 1. How many applications are used in the linked list , No waste of space 2. Head insertion 、 Tail insertion 、 Head deletion 、 Delete as O(1) 3. Quickly insert and delete from anywhere 4. The double linked list needs to traverse every node , Slow access . |
deque( deque ) | Support quick insertion and deletion at both ends , Direct access to any element |
Associate container |
|
set( aggregate ) | Quick search , Duplicate values... Are not allowed |
multiset( Multiple sets ) | Quick search , Duplicate values are allowed |
map( mapping ) | One-to-one mapping , Quick search based on keywords , Duplicate values... Are not allowed |
multimap( Multiple mapping ) | One to many mapping , Quick search based on keywords , Duplicate values are allowed |
Container adapter |
|
stack( Stack ) | Last in, first out (LIFO) |
queue( queue ) | First in, then out (FIFO) |
priority_queue( Priority queue ) | The highest priority element is always the first out of the line |
边栏推荐
- Why are function templates not partial specialization?
- Cloudcompare & PCL point cloud clipping (based on closed surfaces or polygons)
- 叠加阶梯图和线图及合并线图和针状图
- Webrtc advantages and module splitting
- [introduction to SQL in 10 days] day5+6 consolidated table
- AWS saves data on the cloud (3)
- Common faults and solutions of Substation
- Error: `brew cask` is no longer a `brew` command. Use `brew <command> --cask` instead.
- [reprint] STM32 GPIO type
- nuxt3入门
猜你喜欢

Almost union find (weighted union search)

【大案例】学成在线网站

What is the bandwidth of the Tiktok server that can be used by hundreds of millions of people at the same time?
![[untitled]](/img/bb/213f213c695795daecb81a4cf2adcd.jpg)
[untitled]

Privacy computing fat----- offline prediction

Error: `brew cask` is no longer a `brew` command. Use `brew <command> --cask` instead.

电子元器件销售ERP管理系统哪个比较好?

Infinite penetration test

containerd1.5.5的安装

webrtc优势与模块拆分
随机推荐
[go ~ 0 to 1] the next day, June 25, switch statement, array declaration and traversal
【.NET6】gRPC服务端和客户端开发案例,以及minimal API服务、gRPC服务和传统webapi服务的访问效率大对决
Construire le premier réseau neuronal avec pytorch et optimiser
Redis deployment under Linux & redis startup
Tree
Sword finger offer 30 Stack containing min function
Force buckle 1024 video splicing
Love analysis released the 2022 love analysis · it operation and maintenance manufacturer panorama report, and an Chao cloud was strongly selected!
Redis02 -- an operation command of five data types for ending redis (it can be learned, reviewed, interviewed and collected for backup)
40多岁的人如何配置年金险?哪款产品比较合适?
Priority of JS operator
How to suppress SiC MOSFET crosstalk?
Kali Notes(1)
Key points of building fire protection design
VMware Workstation related issues
Lilda low code data large screen, leveling the threshold of data application development
抖音服务器带宽有多大,才能供上亿人同时刷?
Error: `brew cask` is no longer a `brew` command. Use `brew <command> --cask` instead.
抖音服務器帶寬有多大,才能供上億人同時刷?
A - Bi-shoe and Phi-shoe