00 in front
[STL source code analysis] summary note (8): Exploration of red black tree
The content of this article is much simpler on the basis of red and black trees. set and map need to understand their structures. It's best to use them easily in the actual process of using STL.
Because the red black tree is used as the bottom layer, it should be noted that the elements will be sorted automatically.
01 set/multiset
set
The bottom layer of set depends on the red black tree, so it will be sorted automatically according to the key of the element. For set, key is value, and set does not allow two elements to have the same key.
Similarly, we cannot modify the element value of set, which can be seen from the later set iterator.
The structure of set 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 the following points:
- key_type and Value_type is the key itself, and both key and value use a Compare function.
- A rep is defined_ t of type, and this rep_type is rb_tree, the five parameters we mentioned last time are all here.
- key_type and value_ All types are keys. The next parameter refers to how to retrieve the key in value. Here, the identity returns itself, which conforms to the characteristics of set. In the first line of Compare, 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, pass in yourself and return yourself.
Take another look at the iterator of set
typedef typename rep_type::const_iterator iterator;
It's RB_ Const of tree_ Iterator also indicates that the element value cannot be changed at will.
In this way, actually, set is very similar to the container adapter mentioned earlier, because its functions are realized by relying on the red black tree.
multiset
multiset allows duplicate key s, which is also different from set.
And their bottom layers are realized by red and black trees. How to distinguish them?
In fact, the difference lies in the use of the insert function of the red black tree.
set uses the insert of red black tree_ Unique() ensures that the inserted elements are not repeated, while multiset uses insert_equal(), the elements can be the same when inserted.
insert_unique() will not report an error when inserting the same element, but it cannot be inserted successfully.
02 map and multimap
map
The bottom layer of the map is also supported by the red black tree, so it will be sorted automatically according to the key of the element. The special point of map is that all elements are pair ed. Pairs include key and value. Map does not allow two elements to have the same key, and the iterator cannot modify the key, but can modify the value.
The structure of the map 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, note:
- key and data together form value
- When calling the red black tree, key and value are passed in. Note that the value here is pair
- The third parameter is how to get the key from value. For pair, it is the first parameter key, so select1st is called to return the first parameter of pair
Let's take a look at the iterator
typedef typename rep_type::iterator iterator;
The iterator here is not similar to const in set_ Iterator, because value can be modified through iterator, but key cannot be modified.
Therefore, in the above definition, Key is const type.
There is also a map unique operator []
T& operator[](const key_type& k){ return (*((insert(value_type(k,T()))),first)).second; }
Explain this operation briefly. For example, if a map is a, a[i] can pass in a key to find the corresponding value. If it does not exist, the default value is inserted.
multimap
The difference between multimap and map is the same as above, that is, it allows duplicate key values.
map uses the insert of red black tree_ Unique() ensures that the inserted elements are not repeated, while multimap uses insert_equal(), the elements can be the same when inserted.