stl源码剖析 详细学习笔记 set map
//
// set map.cpp
// 笔记
//
// Created by fam on 15/3/23.
//
//
//---------------------------15/03/23----------------------------
//set
{
/*
set概述:
1:所有的元素都会被自动排序,
2:所有的元素只有"键"没有“值”
或者说他们的“值”就是“键”
3:不允许出现两个相同的键值
4:不能通过迭代器改变set的值,因为set的值就是键,
如果可以改变,我们要先删除键,平衡二叉树,再加改变后的键,再平衡
这么做严重破坏了set的组织,iterator也会变得无效,到底是指向之前的位置
还是指向改变后的位置等等。所以stl中不允许改变set的值。
5:set用RB_tree作为底层机制(废话,不然干嘛花大篇幅介绍RB_tree)
*/
//class
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:
//identity<value_type>
可以根据key值得到value值
typedef rb_tree<key_type, value_type,
identity<value_type>, key_compare, Alloc> rep_type;
rep_type t;
public:
//由于上面说的set不能改变值,所以指针和迭代器引用这些
全都用RB_tree中的const类型的来指定
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;
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;
set() : t(Compare()){}
explicit set(const Compare& comp) : t(comp){}
//inset_unique
是不允许相同值出现的插入
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;
}
key_compare key_comp()
const { return t.key_comp();}
//set中的值就是键
所以调用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.end();}
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_interator_bool;
pair<iterator,
bool> inset(const value_type& x)
{
pair<typename rep_type::iterator,
bool> p =t.insert_unique(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_unique((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();}
//
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== __STL_NULL_TMPL_ARGS (const set&,
const set&);
friend bool
operator< __STL_NULL_TMPL_ARGS (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;
}
//
总结
//没什么好总结的,这里只是不断调用底层的东西,没有技术可言
}
//map
{
/*
map概述:
和set不同的就是,多了一个值的概念,每一个键都可以存储一个实值
排序是根据key(键)来排序的。一般都是根据key来取其中的值。
和set一样的原理,我们不能改变键。和set不同的是,可以改变data(实值)
实值只是节点所存储的东西,当然可以改,改了不会影响排序
*/
//struct pair
template<class T1,
class T2>
struct pair
{
typedef T1 first_type;
typedef T2 second_type;
T1 first;
T2 second;
pair() : first(T1()), second(T2()){}
pair(const T1& a,const T2& b) : first(a), second(b){}
};
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;
typedef Compare value_compare;
class value_compare
:
public binary_function<value_type, value_type,
bool>
{
friend class map<Key, T, Compare, Alloc>;
protected:
Compare comp;
value_compare(Compare c) : comp(c) {}
public:
bool operator()(const value_type& x,
const value_type& y)
const
{
return comp(x.first, y.first);
}
};
private:
typedef rb_tree<key_type, value_type,
select1st<value_type>, key_compare, Alloc> rep_type;
rep_type t;
public:
typedef typename rep_type::pointer pointer;
typedef typename rep_type::const_pointer const_pointer;
typedef typename rep_type::reference reference;
typedef typename rep_type::const_reference const_reference;
typedef typename rep_type::iterator iterator;
typedef typename rep_type::const_iterator const_iterator;
typedef typename rep_type::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;
map() : t(Compare()){}
explicit map(const Compare& comp) : t(comp){}
//inset_unique
是不允许相同值出现的插入
template<class InputIterator>
map(InputIterator first, InputIterator last)
: t(Compare()){ t.insert_unique(first, last);}
template<class InputIterator>
map(InputIterator first, InputIterator last,const Compare& comp)
: t(comp){t.insert_unique(first, last);}
map(const set<Key, Compare, Alloc>& x): t(x.t){}
map<Key, Compare, Alloc>&
operator=(const map<Key, Compare, Alloc>& x)
{
t = x.t;
return *this;
}
key_compare key_comp()
const { return t.key_comp();}
value_compare value_comp()
const { return t.value_compare(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.end();}
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);}
//map可以根据
键 来取
值, 如果不存在就插入一个新的
键值对
T&
operator[] (const key_type& k)
{
return (*((insert(value_type(k,T()))).first)).second;
}
//insert erase
pair<iterator,
bool> inset(const value_type& x)
{
pair<typename rep_type::iterator,
bool> p =t.insert_unique(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_unique((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();}
//
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== __STL_NULL_TMPL_ARGS (const set&,
const set&);
friend bool
operator< __STL_NULL_TMPL_ARGS (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;
}
/*
总结:
map和set不同的是有了data这个概念,并且可以更改这个data值了
set插入时插的就是key, map插入时插的是value(键值对)
插入时,函数会调用keyofvalue仿函数取得value的key
之后排序会根据key_compare(也就是模版参数中的Compare)来比较key的大小
最后都是用key来比较的,不同的是set存的值也是key,map存的值是data。
*/
}
//multiset 和multimap
/*
这两个容器和之前的基本相同,不同的就是把inset_unique改成inset_equel
具体的底层实现在RB_tree
中已经实现了,只要调用就行了
*/
stl源码剖析 详细学习笔记 set map的更多相关文章
- stl源码剖析 详细学习笔记 hashtable
//---------------------------15/03/24---------------------------- //hashtable { /* 概述: sgi采用的是开链法完成h ...
- stl源码剖析 详细学习笔记 RB_tree (1)
// // RB_tree_STL.cpp // 笔记 // // Created by fam on 15/3/21. // // #include "RB_tree_STL.h&q ...
- stl源码剖析 详细学习笔记heap
// // heap.cpp // 笔记 // // Created by fam on 15/3/15. // // //---------------------------15/03/15 ...
- stl源码剖析 详细学习笔记 空间配置器
//---------------------------15/04/05---------------------------- /* 空间配置器概述: 1:new操作包含两个阶段操作 1>调 ...
- stl源码剖析 详细学习笔记 算法(1)
//---------------------------15/03/27---------------------------- //算法 { /* 质变算法:会改变操作对象之值 所有的stl算法都 ...
- stl源码剖析 详细学习笔记 算法总览
//****************************基本算法***************************** /* stl算法总览,不在stl标准规格的sgi专属算法,都以 *加以标 ...
- stl源码剖析 详细学习笔记 RB_tree (2)
//---------------------------15/03/22---------------------------- //一直好奇KeyOfValue是什么,查了下就是一个和仿函数差不多 ...
- stl源码剖析 详细学习笔记priority_queue slist
// // priority_queue.cpp // 笔记 // // Created by fam on 15/3/16. // // //------------------------- ...
- stl源码剖析 详细学习笔记stack queue
// // stack.cpp // 笔记 // // Created by fam on 15/3/15. // // //---------------------------15/03/1 ...
随机推荐
- Coursera-AndrewNg(吴恩达)机器学习笔记——第四周编程作业(多分类与神经网络)
多分类问题——识别手写体数字0-9 一.逻辑回归解决多分类问题 1.图片像素为20*20,X的属性数目为400,输出层神经元个数为10,分别代表1-10(把0映射为10). 通过以下代码先形式化展示数 ...
- OpenGL超级宝典笔记——贝塞尔曲线和曲面(转)
http://my.oschina.net/sweetdark/blog/183721 参数方程表现形式 在中学的时候,我们都学习过直线的参数方程:y = kx + b;其中k表示斜率,b表示截距(即 ...
- 使用C#删除一个字符串数组中的空字符串
C#中要如何才能删除一个字符串数组中的空字符串呢?随着微软对C#不断发展和更新,C#中对于数组操作的方式也变得越来越多样化.以往要实现过滤数组中的空字符串,都是需要实行循环的方式来排除和过滤.C#3. ...
- 团队作业——Beta冲刺2
团队作业--Beta冲刺 冲刺任务安排 杨光海天 今日任务:根据冲刺内容,具体分配个人任务,对于冲刺内容做准备 明日任务:图片详情界面的开发 吴松青 今日任务:学习熟悉安卓开发,跟随组员快速了解其代码 ...
- UltraISO制作使用(服务器装机u盘制作)
1.准备工作: 1)U盘一个,需要格式化(大于4G,毕竟ISO文件就已经大于4G了) 2)CentOS7.1 iso文件一个(去这里下载:http://www.centoscn.com/) 3)Ult ...
- sublime设置node.js编译
1. 首先需安装node环境并配置好环境变量,安装教程. 2. 然后在sublime中打开工具(Tools)→编译系统(Build System)→新编译系统(New Build System) 3. ...
- BZOJ4300:绝世好题(DP)
Description 给定一个长度为n的数列ai,求ai的子序列bi的最长长度,满足bi&bi-1!=0(2<=i<=len). Input 输入文件共2行. 第一行包括一个整数 ...
- QGIS(2.18.15 源码)+Qt(5/5.9.3)+VS2015(X64)编译
由于工作要求,今年需要基于Qt搞跨平台的GIS.前期未曾接触过Qt,最近也简单学习了下,开源的QGIS是非常不错的学习资源,下了最新版的QGIS源码,不过在VS2015下却没法直接打开.网上查了很多资 ...
- JS输入框邮箱自动提示(带有demo和源码)
今天在javascriptQQ群里面 有童鞋问到 有没有 "JS输入框邮箱自动提示"插件,即说都找遍了github上源码 都没有看到这样类似的插件,然后我想了下 "JS输 ...
- POJ3468(线段树区间求和+区间查询)
https://vjudge.net/contest/66989#problem/C You have N integers, A1, A2, ... , AN. You need to deal w ...