STL源码剖析(set/map)
SGI STL中set/map底层都是通过RB-tree实现的。
首先看看RB-tree结点的定义
typedef bool __rb_tree_color_type;
const __rb_tree_color_type __rb_tree_red = false;
const __rb_tree_color_type __rb_tree_black = true; // 结点的基类
struct __rb_tree_node_base
{
typedef __rb_tree_color_type color_type;
typedef __rb_tree_node_base* base_ptr; // 关键的4个域
color_type color;
base_ptr parent;
base_ptr left;
base_ptr right; // 返回极值
static base_ptr minimum(base_ptr x)
{
while (x->left != ) x = x->left;
return x;
} static base_ptr maximum(base_ptr x)
{
while (x->right != ) x = x->right;
return x;
}
} // 多了一个value域
template <class Value>
struct __rb_tree_node : public __rb_tree_node_base
{
typedef __rb_tree_node<Value>* link_type;
Value value_field;
};
下图是RB-tree结点跟其迭代器的关系
重点看看__rb_tree_iterator的operator++跟operator--,
实际是调用__rb_tree_base_iterator的increment跟decrement。
可以看出迭代器前移/后移的时候会按key的顺序找到下一个/上一个结点。
(set/map<...>::begin()会返回RB-tree中key最小的结点,因此使用operator++遍历会按key的顺序从小到大遍历结点)
void increment()
{
if (node->right != ) {
node = node->right;
while (node->left != )
node = node->left;
}
else {
base_ptr y = node->parent;
while (node == y->right) {
node = y;
y = y->parent;
}
if (node->right != y)
node = y;
}
} void decrement()
{
if (node->color == __rb_tree_red &&
node->parent->parent == node)
node = node->right;
else if (node->left != ) {
base_ptr y = node->left;
while (y->right != )
y = y->right;
node = y;
}
else {
base_ptr y = node->parent;
while (node == y->left) {
node = y;
y = y->parent;
}
node = y;
}
}
SGI STL中的rb_tree用了一个小trick,就是使用了一个header结点,用来代表整个rb_tree。
该结点与root结点互为父结点,该结点的left指向最左(key最小)的结点,right指向最右(key最大)的结点。
除此之外,该rb_tree的实现跟普通的红黑树类似,详情可以查看:http://www.cnblogs.com/runnyu/p/4679279.html。
template <class Key, class Value, class KeyOfValue, class Compare,
class Alloc = alloc>
class rb_tree {
// rb_tree的基本定义
protected:
typedef __rb_tree_node_base* base_ptr;
typedef __rb_tree_node<Value> rb_tree_node;
typedef simple_alloc<rb_tree_node, Alloc> rb_tree_node_allocator;
public:
typedef Key key_type;
typedef Value value_type;
typedef value_type* pointer;
typedef value_type& reference;
typedef rb_tree_node* link_type;
typedef size_t size_type;
typedef ptrdiff_t difference_type; typedef __rb_tree_iterator<value_type, reference, pointer> iterator; link_type header;
// ... // 主要接口
iterator begin() { return leftmost(); } // 返回最左边的结点(最小key)
iterator end() { return header; } iterator insert_equal(const value_type& x); // 插入元素 并允许键值相同
pair<iterator,bool> insert_unique(const value_type& x); // 插入元素 键值是独一无二的 };
set/multiset
有了rb_tree,set/multiset的实现也只是调用rb_tree的接口而已。
其中set跟multiset不一样的是,set插入的时候调用的是insert_unique(),而multiset调用的是insert_equal()。
下面是给出set的基本定义:
template <class Key, class Compare = less<Key>, class Alloc = alloc>
class set {
public:
typedef Key key_type;
typedef Key value_type; // 使用的value类型跟key一样
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;
public:
// 接口的实现只是对rb_tree的封装 不一一列举了
iterator begin() const { return t.begin(); }
iterator end() const { return t.end(); }
pair<iterator,bool> insert(const value_type& x) {
pair<typename rep_type::iterator, bool> p = t.insert_unique(x);
return pair<iterator, bool>(p.first, p.second);
}
// ...
};
map/multimap
map/mulitmap的实现也是通过调用rb_tree的接口。
map/mulitmap不一样的是,map插入的时候调用的是insert_unique(),而multimap调用的是insert_equal()。
下面是给出map的基本定义:
template <class Key, class T, class Compare = less<Key>, class Alloc
class map {
public:
typedef Key key_type;
typedef T data_type;
typedef T mapped_type;
typedef pair<const Key, T> value_type; // 在rb_tree中value的类型是pair
typedef Compare key_compare;
private:
// select1st直接return T.first 用于rb_tree取到key进行比较大小
typedef rb_tree<key_type, value_type,
select1st<value_type>, key_compare, Alloc> rep_type;
rep_type t;
// ... // 接口只是对rb_tree的封装 就不一一列举了
iterator begin() { return t.begin(); }
iterator end() { return t.end(); }
pair<iterator,bool> insert(const value_type& x) { return t.insert_unique(x); }
// ...
}
另外STL中有未列入标准的hash_set/hash_map以及C++11中的unordered_set/map,底层是使用hashtable实现的。
相比于用rb_tree实现的set/map,它们的插入删除查找操作具有O(1)的时间复杂度(没有冲突情况下),但是它们的元素的顺序是无序的。
STL源码剖析(set/map)的更多相关文章
- STL"源码"剖析-重点知识总结
STL是C++重要的组件之一,大学时看过<STL源码剖析>这本书,这几天复习了一下,总结出以下LZ认为比较重要的知识点,内容有点略多 :) 1.STL概述 STL提供六大组件,彼此可以组合 ...
- 【转载】STL"源码"剖析-重点知识总结
原文:STL"源码"剖析-重点知识总结 STL是C++重要的组件之一,大学时看过<STL源码剖析>这本书,这几天复习了一下,总结出以下LZ认为比较重要的知识点,内容有点 ...
- STL源码剖析 迭代器(iterator)概念与编程技法(三)
1 STL迭代器原理 1.1 迭代器(iterator)是一中检查容器内元素并遍历元素的数据类型,STL设计的精髓在于,把容器(Containers)和算法(Algorithms)分开,而迭代器(i ...
- STL"源码"剖析
STL"源码"剖析-重点知识总结 STL是C++重要的组件之一,大学时看过<STL源码剖析>这本书,这几天复习了一下,总结出以下LZ认为比较重要的知识点,内容有点略 ...
- 《STL源码剖析》相关面试题总结
原文链接:http://www.cnblogs.com/raichen/p/5817158.html 一.STL简介 STL提供六大组件,彼此可以组合套用: 容器容器就是各种数据结构,我就不多说,看看 ...
- 《STL源码剖析》读书笔记
转载:https://www.cnblogs.com/xiaoyi115/p/3721922.html 直接逼入正题. Standard Template Library简称STL.STL可分为容器( ...
- STL源码剖析之组件
本篇文章开始,进行STL源码剖析的一些知识点,后续系列笔记全是参照<STL源码剖析>进行学习记录的 STL在现在的大部分项目中,实用性已经没有Boost库好了,毕竟STL中仅仅提供了一些容 ...
- 面试题总结(三)、《STL源码剖析》相关面试题总结
声明:本文主要探讨与STL实现相关的面试题,主要参考侯捷的<STL源码剖析>,每一个知识点讨论力求简洁,便于记忆,但讨论深度有限,如要深入研究可点击参考链接,希望对正在找工作的同学有点帮助 ...
- 通读《STL源码剖析》之后的一点读书笔记
直接逼入正题. Standard Template Library简称STL.STL可分为容器(containers).迭代器(iterators).空间配置器(allocator).配接器(adap ...
随机推荐
- Linux命令之useradd
useradd [选项] LOGIN(登录名) useradd –D useradd –D [选项] 创建一个新用户或更新默认新用户信息.useradd和adduser命令相同,adduser是use ...
- poj 3132
Sum of Different Primes Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3360 Accepted ...
- 【计算几何】【二分】【随机增量法】hdu6167 Missile Interception
n个半径为R的圆是否有公共部分,等价于询问是否存在一个半径小于R的圆,能覆盖所有n个圆的圆心. 对这n个点求最小圆覆盖即可.从网上扒了个随机增量法的代码. 这样算上二分,复杂度就是nlogn了. #i ...
- 【深搜+set使用学习】POJ3050-Hopscotch
[题目大意] 给出一个5*5的方格,求出从任意一点出发走6步组成的不同序列数. [思路] dfs的水题,当作set使用方法的初次学习.每次从任意一点出发进行一次dfs,将序列加入set,最后输出set ...
- Perl中的数组&哈希应用
哈希和数组是Perl中较为常用的结构,本文则重点讨论数组和哈希的一些基本用法,供广大喜爱Perl的同学们交流学习. 哈希 Perl中的哈希表类似于Python中的字典结构,由(键=>值)对构成, ...
- 统计个位数的数目 Exercise07_07
/** * @author 冰樱梦 * 时间:2018年下半年 * 题目:统计个位数的数目 * */ public class Exercise07_07 { public static void m ...
- Problem C: 调用函数,求a+aa+aaa+....+aa...aa(n个a)
#include <stdio.h> int fn(int a,int n)//定义函数 { ; ;i<=n;i++) { m=m+a;//当a=3时,m=3,然后a=30,m=33 ...
- 源码安装python及paramikon的初步试用
Auth: jin Date: 20140314 OS: CentOS release 5.5 (Final) 默认2.4版本 莫 1.download wget http://www.python. ...
- 让你的WPF程序在Win7下呈现Win8风格主题
今天在Win8下使用了一个我之前写的一个WPF程序的时候,发现现在也支持Win8效果了(记得以前的.net 4.0的版本是不支持的).由于WPF的控件是自绘的,并不受系统主题所控制,也就是说.net ...
- minGW cygwin gnuwin32
首先,三个的官方网站分别是: minGW:http://www.mingw.org cygwin: http://www.cygwin.com gnuwin32: https://sourcefo ...