当前位置:网站首页>[STL source code analysis] summary note (2): overview of containers
[STL source code analysis] summary note (2): overview of containers
2022-06-11 07:16:00 【Janvis of the stark family】
00 Write it at the front
Containers (containers) yes STL An important part of , It is also a part worthy of our in-depth study . Various vector、map、set The use of has greatly improved our efficiency in solving problems .
Each container has its own unique implementation and some key points that we need to understand , These will be the focus of the article .
01 Structure and classification of containers
summary
Containers can be roughly divided into sequential containers (Sequence), Associative containers (Associative) And unordered containers (Unordered), Unordered containers can also be classified as associative containers .

classification
Sequential containers (Sequence)
- array: Array , Is a fixed size structure , Static space , The size cannot be changed after configuration .
- vector: The dynamic array , Linear continuous space with unidirectional openings , Dynamically configurable space , The principle is to double the capacity each time , Then copy the original spatial data to the new space .
- deque: It is a linear continuous space with two-way openings , You can insert and delete at the beginning and end .
- list: Chain table structure , Not a linear continuous space , Use pointer addressing , Here refers to a two-way linked list .
- forward-list: One way linked list
- stack/queue: Stacks and queues , The bottom layer is made of two-way openings deque Realized after packaging .
Associative containers (Associative)
- set/multiset: A structure based on red and black trees , It can store key. about set Come on key Not to be repeated ,multiset Of key Can be repeated .
- map/multimap: A structure based on red and black trees , It can store key and value, about map Come on key Not to be repeated ,multimap Of key Can be repeated .
- unordered set/multiset: A structure based on a hash table , It can store key. about set Come on key Not to be repeated ,multiset Of key Can be repeated .
- unordered map/multimap: A structure based on a hash table , It can store key and value, about map Come on key Not to be repeated ,multimap Of key Can be repeated .
Contrast relationship :
| aggregate | Underlying implementation | Is it orderly | Whether the value can be repeated | Can I change the value | The query efficiency | Efficiency of addition and deletion |
|---|---|---|---|---|---|---|
| std::set | Red and black trees | Orderly | no | no | O(logn) | O(logn) |
| std::multiset | Red and black trees | Orderly | yes | no | O(logn) | O(logn) |
| std::unordered_set | Hashtable | disorder | no | no | O(1) | O(1) |
| mapping | Underlying implementation | Is it orderly | Whether the value can be repeated | Can I change the value | The query efficiency | Efficiency of addition and deletion |
|---|---|---|---|---|---|---|
| std::map | Red and black trees | key Orderly | key Do not repeat | key Do not modify the | O(logn) | O(logn) |
| std::multimap | Red and black trees | key Orderly | key repeatable | key Do not modify the | O(logn) | O(logn) |
| std::unordered_map | Hashtable | key disorder | key Do not repeat | key Do not modify the | O(1) | O(1) |
02 The relationship between containers
Let's first look at a diagram :

The indented display here represents the relationship between the base layer and the derived layer , Derivation is not inheritance , But compound .
such as heap and priority There are vector As the base .set、map There are red and black trees as the base .
There are also some non-standard containers inside , such as slist,hash At the beginning, wait . stay C++ 11 in slist Renamed as forward-list,hash All the beginning should be changed to unordered start .
notes : there sizeof() Bury a small foreshadowing , There will be specific size comparison later . in addition GNU4.9 There are many designs that have become complicated , For example, a lot of inheritance is used , But the actual function is not very different .
03 summary
After knowing the general contents of the container , We can learn from the most familiar vector Let's take a look at the internal implementation details .
There are many things worth learning in other containers , such as list The iterator will be reflected in (iterators) The subtlety of design , stay set and map The implementation of red and black trees will be reflected in , Follow up with a separate summary .
边栏推荐
- Shangtang technology has actively resumed work and will vigorously invest in the capacity and deployment of the digital sentry
- Xunwei dry goods | Ruixin micro rk3568 development board TFTP & NFS writing (Part 1)
- JVM Learning record (7) - - class Loading Process and parent delegation Model
- Shuttle container component
- Difference between byte and bit
- QT 基于QScrollArea的界面嵌套移动
- Promises/a+ standard Chinese Translation
- 1266_FreeRTOS调度器启动代码实现分析
- Janus feature draft
- Janus feature草稿
猜你喜欢

一、SQLServer2008安装(带密码)、创建数据库、C#窗体项目测试

The gap between the parent box and the child box

软件测试周刊(第75期):唯有平视,才能看见真实的自己。

1、 Sqlserver2008 installation (with password), database creation, C form project test

Modular notes

Gobang interface of mobile console (C language)

Leetcode hot topic 100 topic 21-25 solution

@Jsonproperty annotation

No response from win10 explorer when dragging files

matplotlib的cmap
随机推荐
【CF #277.5 (Div. 2)】B. BerSU Ball
[并发进阶]——线程池总结
Start the Nacos server of shell script
Listen to the left width of the browser to calculate the distance
Biological sequence intelligent analysis platform blog (1)
Mistakes in Niuke JS exercise
Regular Expression Matching
Leetcode hot topic 100 topic 6-10 solution
Dynamically change the direction of this
Oracle pl/sql these query results cannot be updated. Please include ROWID or use Select For update
Promise. All capture error
1266_ Implementation analysis of FreeRTOS scheduler startup code
Leetcode-647.Palindromic Substrings
Error occurred in pycharm DeprecatedEnv: Env FrozenLake-v0 not found (valid versions include [‘FrozenLake-v1‘])
Janus feature草稿
Modular notes
[Xunwei dry goods] opencv test of Godson 2k1000 development board
MS office level II wrong question record [8]
Completed in May, 22
Leetcode hot topic 100 topic 21-25 solution