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)的更多相关文章

  1. STL"源码"剖析-重点知识总结

    STL是C++重要的组件之一,大学时看过<STL源码剖析>这本书,这几天复习了一下,总结出以下LZ认为比较重要的知识点,内容有点略多 :) 1.STL概述 STL提供六大组件,彼此可以组合 ...

  2. 【转载】STL"源码"剖析-重点知识总结

    原文:STL"源码"剖析-重点知识总结 STL是C++重要的组件之一,大学时看过<STL源码剖析>这本书,这几天复习了一下,总结出以下LZ认为比较重要的知识点,内容有点 ...

  3. STL源码剖析 迭代器(iterator)概念与编程技法(三)

    1 STL迭代器原理 1.1  迭代器(iterator)是一中检查容器内元素并遍历元素的数据类型,STL设计的精髓在于,把容器(Containers)和算法(Algorithms)分开,而迭代器(i ...

  4. STL"源码"剖析

    STL"源码"剖析-重点知识总结   STL是C++重要的组件之一,大学时看过<STL源码剖析>这本书,这几天复习了一下,总结出以下LZ认为比较重要的知识点,内容有点略 ...

  5. 《STL源码剖析》相关面试题总结

    原文链接:http://www.cnblogs.com/raichen/p/5817158.html 一.STL简介 STL提供六大组件,彼此可以组合套用: 容器容器就是各种数据结构,我就不多说,看看 ...

  6. 《STL源码剖析》读书笔记

    转载:https://www.cnblogs.com/xiaoyi115/p/3721922.html 直接逼入正题. Standard Template Library简称STL.STL可分为容器( ...

  7. STL源码剖析之组件

    本篇文章开始,进行STL源码剖析的一些知识点,后续系列笔记全是参照<STL源码剖析>进行学习记录的 STL在现在的大部分项目中,实用性已经没有Boost库好了,毕竟STL中仅仅提供了一些容 ...

  8. 面试题总结(三)、《STL源码剖析》相关面试题总结

    声明:本文主要探讨与STL实现相关的面试题,主要参考侯捷的<STL源码剖析>,每一个知识点讨论力求简洁,便于记忆,但讨论深度有限,如要深入研究可点击参考链接,希望对正在找工作的同学有点帮助 ...

  9. 通读《STL源码剖析》之后的一点读书笔记

    直接逼入正题. Standard Template Library简称STL.STL可分为容器(containers).迭代器(iterators).空间配置器(allocator).配接器(adap ...

随机推荐

  1. 关于matplotlib,你要的饼图在这里

    Table of Contents 1  官方Demo 2  将实际数据应用于官方Demo 3  一些改善措施 3.1  重新设置字体大小 3.2  设置显示颜色,Method 1: 3.3  设置显 ...

  2. 【转】【python】装饰器的原理

    写在前面: 在开发OpenStack过程中,经常可以看到代码中的各种注解,自己也去查阅了资料,了解了这是python中的装饰器,因为弱类型的语言可以将函数当成返回值返回,这就是装饰器的原理. 虽然说知 ...

  3. If you want to allow applications containing errors to be published on the server

    If you want to allow applications containing errors to be published on the server, enable the Allow ...

  4. 对Webservice的理解

    eb   Service使用的是   SOAP   (Simple   Object   Access   Protocol)协议 soap协议只是用来封装消息用的.封装后的消息你可以通过各种已有的协 ...

  5. NOIP 2006 提高组 t1 能量项链

    题目描述 在Mars星球上,每个Mars人都随身佩带着一串能量项链.在项链上有N颗能量珠.能量珠是一颗有头标记与尾标记的珠子,这些标记对应着某个正整数.并且,对于相邻的两颗珠子,前一颗珠子的尾标记一定 ...

  6. 「HNOI2013」游走

    「HNOI2013」游走 题目描述 一个无向连通图,顶点从 \(1\) 编号到 \(N\) ,边从 \(1\) 编号到 \(M\) .小 \(Z\) 在该图上进行随机游走,初始时小 \(Z\) 在 \ ...

  7. [BZOJ2109][NOI2010]航空管制(贪心+拓扑)

    2109: [Noi2010]Plane 航空管制 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1227  Solved: 510[Submit][ ...

  8. [BZOJ5011][JXOI2017]颜色

    5011: [Jx2017]颜色 Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 84  Solved: 46[Submit][Status][Disc ...

  9. 【数论】【欧拉函数】CDOJ1724 为了我们心爱的京电

    京州电子科技大学遭遇废校危机,为了保护我们心爱的学校,N位魔法少女站了出来,她们能做的就是……成为偶像! 每个魔法少女都拥有一定的人气,他们中的每个人的人气计算方式如下: 假设某个魔法少女的学号为a, ...

  10. 【动态规划】【单调队列】tyvj1305 最大子序和

    http://blog.csdn.net/oiljt12138/article/details/51174560 单调队列优化dp #include<cstdio> #include< ...