当前位置:网站首页>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 |
边栏推荐
- 整数划分
- Potential safety hazards in elderly care facilities
- Error: `brew cask` is no longer a `brew` command. Use `brew <command> --cask` instead.
- Force buckle 1024 video splicing
- centos mysql5.5配置文件在哪
- Trailing Zeroes (II)
- [learning notes] search
- Selenium+chromedriver cannot open Google browser page
- 抖音服务器带宽有多大,才能供上亿人同时刷?
- Zhejiang energy online monitoring and management system
猜你喜欢
随机推荐
[introduction to SQL in 10 days] day5+6 consolidated table
Wasmedge 0.10.0 release! New plug-in extension mechanism, socket API enhancement, llvm 14 support
Anniversary party
Analysis of prepaid power purchase device
[untitled]
CloudCompare&PCL 点云SVD分解
Force buckle 1884 Egg drop - two eggs
电子元器件销售ERP管理系统哪个比较好?
[go ~ 0 to 1] the third day June 27 slice, map and function
Not so Mobile
Redis02 -- an operation command of five data types for ending redis (it can be learned, reviewed, interviewed and collected for backup)
CloudCompare&PCL 点云裁剪(基于封闭曲面或多边形)
Almost union find (weighted union search)
NPM clean cache
【Go ~ 0到1 】 第三天 6月27 slice,map 与 函数
Key points of building fire protection design
Why are function templates not partial specialization?
RAC enable archive log
Webrtc advantages and module splitting
Build an integrated kubernetes in Fedora









