当前位置:网站首页>[STL source code analysis] summary notes (9): set/multiset and map/multimap
[STL source code analysis] summary notes (9): set/multiset and map/multimap
2022-06-11 07:17:00 【Janvis of the stark family】
00 Write it at the front
【STL Source analysis 】 Summary notes (8): Red and black trees (RB-tree) To explore the
The content of this article is much simpler on the basis of red and black trees .set and map Need to understand its structure , In actual use STL In the process, it is better to use it easily .
Because red and black trees are used as the bottom layer , So notice that the elements are automatically sorted .
01 set/multiset
set
set The bottom layer of the tree is supported by red and black trees , So it will be based on the key Automatic sorting . about set Come on ,key Namely value, and set Two elements are not allowed to have the same key.
Empathy , We can't change it either set The element value of , This can be seen from the back set Of iterator I can see .
set The structure is as follows :
template <class Key,class Compare=less<Key>,class Alloc=alloc>
class set{
public:
typedef Key key_type;
typedef Key value_type;
typedef Compare key_compare;
typedef Compare Value_compare;
private:
typedef rb_tree<key_type,value_type,identity<value_type>.key_compare,Alloc>rep_type;
rep_type t;
}
Focus on these points :
- key_type and Value_type All are key In itself , and key and value It's all in one Compare Functional .
- Defined a rep_type Type of t, And this rep_type Namely rb_tree, The five parameters we mentioned last time are all here .
- key_type and value_type All are key, The next parameter refers to how to get value Medium key, there identity Return itself , accord with set Characteristics of . and Compare In the first line, you can see that the default is incremental sorting .
identity Is defined as follows :
template<class T>
struct identity:public unary_function<T,T>{
const T& operator()(const T& x)const{return x;}
}
That is to say, it is introduced into oneself , Return to yourself .
Look again. set Of iterator
typedef typename rep_type::const_iterator iterator;
yes rb_tree Of const_iterator, It also shows that the element value cannot be changed at will .
In fact set Much like what we mentioned earlier container adapter( Container adapter ), Because its functions are realized by red black tree .
multiset
multiset allow key repeat , This is also with set The difference between .
And their bottom layer is realized by red black tree , How to distinguish them ?
In fact, the difference lies in the red and black trees insert Use of functions .
set The red black tree is used insert_unique(), Ensure that the inserted elements are not repeated , and multiset It uses insert_equal(), The elements can be the same when inserting .
insert_unique() No error will be reported when inserting the same element , It just can't be inserted successfully .
02 map and multimap
map
map The bottom layer of the tree is also supported by red and black trees , So it will be based on the key Automatic sorting .map The special point is that all the elements are in pairs (pair),pair Include key and value,map Two elements are not allowed to have the same key, and iterator It cannot be modified key, But it can be modified value.
map The structure is as follows :
template <class Key,class T,class Compare=less<Key>,class Alloc=alloc>
class map{
public:
typedef key key_type;
typedef T data_type;
typedef T mapped_type;
typedef pair<const Key,T> value_type;
typedef Compare key_compare;
...
private:
typedef rb_tree<key_type,value_type,select1st<value_type>,key_compare,Alloc>rep_type;
rep_type t;
}
From top to bottom, you should pay attention to :
- key and data Together make up value
- When calling red black tree , Into key and value, Pay attention here value yes pair
- The third parameter is how to start from value To take key, about pair It is the first parameter key, So I called select1st return pair Of first
Let's take a look at iterator
typedef typename rep_type::iterator iterator;
there iterator It's not like set Medium const_iterator, Because it is allowed to pass iterator modify value, however key Do not modify the .
So in the above definition Key yes const Type of .
One more map Unique operator []
T& operator[](const key_type& k){
return (*((insert(value_type(k,T()))),first)).second;
}
Explain this operation briefly . Let's say I have a map yes a, that a[i] You can bring in key To find the corresponding value. If it doesn't exist , Then the default value will be inserted .
multimap
multimap And map The difference is the same as above , Allow key value repetition .
map The red black tree is used insert_unique(), Ensure that the inserted elements are not repeated , and multimap It uses insert_equal(), The elements can be the same when inserting .
边栏推荐
- Android and IOS reverse analysis / security detection / penetration testing framework
- CMAP of Matplotlib
- Outer margin collapse
- P3811 [template] multiplicative inverse
- 1300. the array closest to the target value after transforming the array and
- Xunwei dry goods | Ruixin micro rk3568 development board TFTP & NFS writing (Part 1)
- Leetcode-647.Palindromic Substrings
- Create a form whose client area is 800 pixels by 600 pixels
- JVM Learning record (7) - - class Loading Process and parent delegation Model
- pycharm出现error.DeprecatedEnv: Env FrozenLake-v0 not found (valid versions include [‘FrozenLake-v1‘])
猜你喜欢

教育专家王中泽老师多年经验分享:家庭教育不是附庸品
![[deploy private warehouse based on harbor] 4 push image to harbor](/img/af/8e28b229d94f3e6eab02308b69dc74.jpg)
[deploy private warehouse based on harbor] 4 push image to harbor

First day of database

资深OpenStacker - 彭博、Vexxhost升级为OpenInfra基金会黄金成员

Leetcode-104. Maximum Depth of Binary Tree

WPF data binding (IV)

Records how cookies are carried in cross domain requests
![[advanced concurrency] - thread pool summary](/img/69/dc8146dafc30f8a8efa012b67aa05c.png)
[advanced concurrency] - thread pool summary

QT 基于QScrollArea的界面嵌套移动

Multi thread review summary parsing volatile keyword
随机推荐
P1390 sum of common divisors (Mobius inversion)
Niuke wrong question 3.1
Oracle pl/sql these query results cannot be updated. Please include ROWID or use Select For update
Installation de SQL Server 2008 (avec mot de passe), création d'une base de données, test de projet de formulaire C
【CF#388 (Div. 2)】A. Bachgold Problem
Notes on learning Es5 and ES6
JVM学习记录(七)——类加载过程与双亲委派模型
Smart pointer (simple version)
AtomicInteger原子操作类
. Net C Foundation (6): namespace - scope with name
Interview question 17.08 Circus tower
Records how cookies are carried in cross domain requests
QT 基于QScrollArea的界面嵌套移动
商汤科技积极复工,将大力投入数字哨兵的产能和部署
Leetcode hot topic 100 topic 11-15 solution
Gobang interface of mobile console (C language)
【CF#654 (Div. 2)】A. Magical Sticks
Education expert wangzhongze solves students' problems with one move
Leetcode hot topic 100 topic 6-10 solution
软件测试周刊(第75期):唯有平视,才能看见真实的自己。