c++版Pair实现
来源:互联网 发布:招商银行云计算 编辑:程序博客网 时间:2024/06/16 12:45
写在前面:小生纯业余选手,开此博仅仅是为了积累,纯当笔记来用。如有看官光临小生博客,请不要相信我的代码就是正确的。如果您发现了错误也恳请耽误您一点时间,请您在下面指出来,不胜感激!
这里的pair与SGI STL里边的pair一样都是键值对,有的书上也叫“字典”(dictionary),不管这种结构叫什么,它都是为了更好地管理数据。在实现的时候遇到了各种各样的麻烦,最后不得不搬出书上的代码。这里的实现要包含list代码文件,键值对是作为一个单元放在某一容器中,下面代码选择的是list容器。
#include "list.h"using namespace std;template<typename Key,typename E>class Dictionary{private:void operator=(const Dictionary&){}Dictionary(const Dictionary&){}public:Dictionary(){}~Dictionary(){}virtual void clear() = 0;virtual void insert(const Key& k,const E& e) = 0;virtual E remove(const Key& k) = 0;virtual E removeAny() = 0;virtual E find(const Key&)const = 0;virtual int size() = 0;};template<typename Key,typename E>class KVpair{private:Key k;E e;public:KVpair(){}KVpair(Key kval,E eval):k(kval),e(eval){}KVpair(const KVpair& o):k(o.k),e(o.e){}void operator =(const KVpair& o){k = o.k;e = o.e;}Key key(){return k;}void setKey(Key ink){k = ink;}E value(){return e;}};template<typename Key,typename E>class UALdict : public Dictionary<Key,E>{private:AList<KVpair<Key,E> >* list;public:UALdict(int size = 10){list = new AList<KVpair<Key,E> >(size);}~UALdict(){delete list;}void clear(){list->clear();}void insert(const Key& k,const E& e){KVpair<Key,E> temp(k,e);list->append(temp);}E remove(const Key &k){E temp = find(k);if(temp != NULL)list->remove();return temp;}E removeAny(){assert(size() != 0);list->moveToEnd();list->prev();KVpair<Key,E> e = list->remove();return e.value();}E find(const Key& k)const{for(list->moveToStart();list->currPos() < list->length();list->next()){KVpair<Key,E> temp = list->getValue();if(k == temp.key())return temp.value();}return NULL;}int size(){return list->length();}};template<typename Key,typename E>class SAList : protected AList<KVpair<Key,E> >{public:SAList(int size = 10):AList<KVpair<Key,E> >(size){}~SAList(){}void insert(KVpair<Key,E>& it){KVpair<Key,E> curr;for(moveToStart();currPos() < length();next()){curr = getValue();if(curr.key()>it.key())break;}AList<KVpair<Key,E> >::insert(it);}AList<KVpair<Key,E> >::clear();AList<KVpair<Key,E> >::remove();AList<KVpair<Key,E> >::moveToStart();AList<KVpair<Key,E> >::moveToEnd();AList<KVpair<Key,E> >::prev();AList<KVpair<Key,E> >::next();AList<KVpair<Key,E> >::length();AList<KVpair<Key,E> >::currPos();AList<KVpair<Key,E> >::moveToPos();AList<KVpair<Key,E> >::getValue();};template<typename Key,typename E>class SALdict : public Dictionary<Key,E>{private:SAList<Key,E>* list;public:SALdict(int size = 10){list = new SAList<Key,E>(size);}~SALdict(){}void clear(){list->clear();}void insert(const Key& k,const E& e){KVpair<Key,E> temp(k,e);list->insert(temp);}E remove(const Key& k){E temp = find(k);if(temp != NULL)list->remove();return temp;}E removeAny(){assert(size() != 0);list->moveToEnd();list->prev();KVpair<Key,E> e = list->remove();return e.value();}E find(const Key& k)const{int l = -1;int r = list->length();while (l+1 != r){int i = (l+r)/2;list->moveToPos(i);KVpair<Key,E> temp = list->getValue();if(k < temp.key())r = i;if(k == temp.key())return temp.value();if(k > temp.key())l = i;}return NULL;}int size(){return list->length();}};
0 0
- c++版Pair实现
- 【C/C++】STL--pair
- C++pair容器
- C++STL学习Pair
- c++pair的用法
- C++pair的用法
- pair (c++)
- [C++]Pair简介
- [C++]STL pair
- C++pair对组
- c++pair基本用法
- pair实现priority_queue
- C++,map和pair用法
- CodeForces 160C Find Pair
- c++中pair的实现
- closest pair 算法的实现
- 用JAVA实现PAIR结构
- pair
- switch语句的解析
- 单例模式的作用以及适用范围 以及创建步骤
- 从Cocos2d-x学习OpenGL -- Cocos2d-x渲染结构
- 面向接口编程_从List list = new ArrayList()谈起
- Linux内存系列文章
- c++版Pair实现
- LeetCode | Word Break
- 自写打印日历类
- map-reduce的八个流程
- AbstractQueuedSynchronizer实现源码解析(三)
- javascript变量作用域
- 用单例模式来设计一个PHP数据库类
- 仪器、界面水有多深(2)
- seekbar介绍及其例子