STL之set实现
2015-10-17 08:52
387 查看
#ifndef __SET_H #define __SET_H #include"rbt.h" #include<functional> 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: template<class T> struct identity :public std::unary_function<T, T> { const T& operator()(const T& x)const{ return x; } }; typedef rb_tree<key_type, value_type, identity<value_type>, key_compare, Alloc> rep_type; rep_type t; public: 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::size_type size_type; typedef typename rep_type::difference_type difference_type; 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; } key_compare key_comp()const{ return t.key_comp(); } value_compare value_comp()const{ return t.key_comp(); } iterator begin(){ return t.begin(); } iterator end(){ 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(); } typedef std::pair<iterator, bool> pair_iterator_bool; std::pair<iterator, bool> insert(const value_type& x) { std::pair<typename rep_type::iterator,bool> p = t.insert_unique(x); return std::pair<iterator, bool>(p.first, p.second); } 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 lower_bound(const key_type& x)const { return t.lower_bound(x); } iterator upper_bound(const key_type& x)const { return upper_bound(x); } std::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; } #endif
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- 解析C++中派生的概念以及派生类成员的访问属性
- C++ Custom Control控件向父窗体发送对应的消息