Chapter 5: 关联式容器之 set 和 multiset
来源:互联网 发布:南京黑马程序员培训班 编辑:程序博客网 时间:2024/05/24 23:14
一:set
1:set 的特性是所有元素都会根据元素的键值自动排序,并且 set 的实值就是键值,键值就是实值,同时 set 不允许两个元素有相同的键值;
2:由于 set 元素值就是键值,而键值关系到 set 元素的排列规则,如果任意改变 set 元素值,会严重破坏 set 组织的,所以我们不可以通过 set 的迭代器改变 set 的元素值;
3:由于红黑树(RB-tree)是一种平衡二叉搜索树,自动排序的效果很不错,所以标准的 STL set 即以 RB-tree 为底层机制。
4:STL set 源代码如下:
//缺省情况下采用递增排序template <class Key, class Compare = less<Key>, class Alloc = alloc>class set {public: //typedefs: typedef Key key_type; typedef Key value_type; //注意,以下 key_compare 和 value_compare 使用同一个比较函数 typedef Compare key_compare; typedef Compare value_compare;private: //注意,以下的 identity 定义于<stl_function.h>,其定义如下: /* template <class T> struct identity: public unary_function<T, T> { const T& operator()(const T& x) { return x; } }; */ typedef rb_tree<key_type, value_type, identity<value_type>, key_compare, Alloc> rep_type; rep_type t; //采用红黑树(RB-tree)来表现 setpublic: typedef typename rep_type::const_pointer pointer; typedef typename rep_type::const_pointer const_pointer; typedef typename rep_type::const_reference reference; typedef typename rep_type::const_reference const_reference; typedef typename rep_type::const_iterator iterator; //注意,上一行,iterator 定义为 RB-tree 的const_iterator,这表示 set 的 //迭代器无法执行写入操作 typedef typename rep_type::const_iterator const_iterator; typedef typename rep_type::const_reverse_iterator reverse_iterator; typedef typename rep_type::const_reverse_iterator const_reverse_iterator; typedef typename rep_type::size_type size_type; typedef typename rep_type::difference_type difference_type; //alocation/deallocation //注意,set 一定使用 RB-tree 的 insert_unique() 而非 insert_equal() //multiset 才使用 RB-tree 的 insert_equal() //因为 set 不允许相同键值存在,multiset 才允许相同键值存在 set() :t(compare()) {} explicit set(const Compare& comp) : t(comp) {} template <class InputIterator> set(InputIterator first, InputIterator last) : t(compare()) { t.insert_unique(first, last); } template <class InputIterator> set(InputIterator first, InputIterator last, const Compare& comp) : t(comp) { t.insert_unique(first, last); } set(const set<Key, Compare, Alloc>& x) : t(x.t) {} set<Key, Compare, Alloc>& operator=(const set<Key, Compare, Alloc>& x) { t = x.t; return *this; } //以下所有的 set 操作行为,RB-tree 都以提供,所以 set 只要传递调用即可 // accessors key_compare key_comp() const { return t.key_comp(); } //以下注意,set 的 value_comp() 事实上为 RB-tree 的 key_comp() value_compare value_comp() const { return t.key_comp(); } iterator begin() const { return t.begin(); } iterator end() const { return t.end(); } reverse_iterator rbegin() const { return t.rbegin(); } reverse_iterator rend() const { return t.rend(); } bool empty() const { return t.empty(); } size_type size() const { return t.size(); } size_type max_size() const { return t.max_size(); } void swap(set<Key, Compare, Alloc>& x) { t.swap(x.t); } //insert / erase typedef pair<iterator, bool> pair_iterator_bool; pair<iterator, bool> insert(const value_type& x) { pair<typename rep_type::iterator, bool> p = t.insert_unqiue(x); return pair<iterator, bool>(p.first, p.second); } iterator insert(iterator position, const value_type& x) { typedef typename rep_type::iterator rep_iterator; return t.insert((rep_iterator&)position, x); } template <class InputIterator> void insert(InputIterator first, InputIterator last) { t.insert_unique(first, last); } void erase(iterator position) { typedef typename rep_type::iterator rep_iterator; t.erase((rep_iterator&)position); } size_type erase(const key_type& x) { return t.erase(x); } void erase(iterator first, iterator last) { typedef typename rep_type::iterator rep_iterator; t.erase((rep_iterator&)first, (rep_iterator&)last); } void clear() { t.clear(); } // set operations iterator find(const key_type& x) const { return t.find(x); } size_type count(const key_type& x) const { return t.count(x); } iterator lower_bound(const key_type& x) const { return t.lower_bound(x); } iterator upper_bound(const key_type& x) const { return t.upper_bound(x); } pair<iterator, iterator> equal_range(const key_type& x) const { return t.equal_range(x); friend bool operator==(const set&, const set&); friend bool operator<(const set&, const set&);};template <class Key, class Compare, class Alloc>inline bool operator==(const set<Key, Compare, Alloc>& x, const set<Key, Compare, Alloc>& y){ return x.t == y.t;}template <class Key, class Compare, class Alloc>inline bool operator<(const set<Key, Compare, Alloc>& x, const set<Key, Compare, Alloc>& y){ return x.t < y.t;}
二:multiset
1:multiset 的特性及用法和 set 完全相同,唯一的差别在于它允许键值重复,因此它的插入采用的是底层机制 RB-tree 的insert_equal()
函数而非insert_unique()
;
2:下面是 multiset 的源代码提要,只列出了与 set 的不同之处:
template <class Key, class Compare = less<Key>, class Alloc = alloc>class multiset {public: // typedefs ...//(与set 相同) // allocation/deallocation //注意,multiset 一定要使用 insert_equal() 而不适用 insert_unqiue() // set 才使用 insert_unique() template <class InputIterator> multiset(InputIterator first, InputIterator last) : t (Compare()) { t.insert_equal(first, last); } template <class InputIterator> multiset(InputIterator first, InputIterator last, const Compare& comp) : t(comp) { t.insert_equal(first, last); } ... //(其它与 set 相同) //insert / erase iterator insert(const value_type& x) { return t.insert_equal(x); } iterator insert(iterator position, const value_type& x) { typedef typename rep_type::iterator rep_iterator; return t.insert_equal((rep_iterator&)position, x); } template <class InputIterator> void insert(InputIterator first, InputIterator last) { t.insert_equal(first, last); } ... //(其它与 set 相同)};
0 0
- Chapter 5: 关联式容器之 set 和 multiset
- 【STL源码剖析读书笔记】【第5章】关联式容器之set、map、multiset和multimap
- 关联式容器set/multiset
- STL容器-关联式容器set/multiset
- C++容器---关联式容器<set>&<multiset>
- 关联容器之set与multiset
- 三 关联式容器(一)set和multiset
- 【深度探索STL】关联式容器set和multiset
- (关联容器)set 和 multiset
- STL源码剖析-关联式容器之set,map,multiset和multimap
- Chapter 16.关联容器multiset
- 5 关联式容器-set map multiset multimap
- C++关联式容器set,multiset
- Chapter 5:关联式容器之 map 和 multimap
- STL关联容器之multiset和multimap
- Chapter 5: 关联式容器之 RB_tree
- Chapter 5: 关联式容器之 hashtable
- C++之set和multiset容器初学
- linux的启动过程与系统调用过程
- Unity引擎(物理引擎上)
- jQuery tab切换收集
- LeetCode-Hash-242. Valid Anagram
- 第一次写博客
- Chapter 5: 关联式容器之 set 和 multiset
- 欢迎使用CSDN-markdown编辑器
- webmagic实战使用
- 洛谷 1196 银河英雄传说 并查集
- 实现卡片布局,左右滑动
- 洛谷P1147 连续自然数和
- 洛谷 P3366 【模板】最小生成树
- HTTP Request 和Response
- Python 设置系统默认编码