⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dictionary.h

📁 班级通讯录c++编写
💻 H
字号:
#include <iostream.h>
#include <assert.h>
template <class KEY, class VAL> class association
{
public: VAL valueField;
		association(KEY initialKey, VAL initialValue);
		void operator=(association<KEY,VAL>&);
		void operator=(VAL value);
		int operator==(const association<KEY,VAL>&);
		int operator==(const KEY & key);
		KEY key()const;
		VAL value()const;
protected: const KEY keyField;
};

template <class KEY, class VAL> association<KEY,VAL>::
association(KEY initialKey, VAL initialValue):keyField(initialKey),valueField(initialValue){};

template <class KEY, class VAL> void association<KEY,VAL>::
operator=(association<KEY,VAL> &v){ keyField=v.keyfield;  valueField=v.valueField; }

template <class KEY, class VAL> void association<KEY,VAL>::
operator=(VAL value){valueField=value;}

template <class KEY, class VAL> int association<KEY,VAL>::
operator==(const association<KEY,VAL> &v){return keyField==v.keyfield;}

template <class KEY, class VAL> int association<KEY,VAL>::
operator==(const KEY & key){return keyField==key;}

template <class KEY, class VAL> KEY association<KEY,VAL>::key()const
{return keyField;}

template <class KEY, class VAL> VAL association<KEY,VAL>::value()const
{return valueField;}
///////////////////////////////////////////////////////////////
template <class KEY, class VAL> class dictionary
{  
public: virtual int isEmpty()const=0;
		virtual int includesKey(KEY key)=0;
        virtual VAL & operator[](KEY key)=0;
        virtual void removeKey(KEY key)=0;
        virtual void deleteAllValues()=0;
        virtual void setInitValue(VAL initValue)=0;
private:virtual association<KEY,VAL>* associatedWith(KEY)=0;
};
////////////////////////////////////////////////////////////
template<class KEY,class VAL>class dictionaryListIterator;
template <class KEY, class VAL> class dictionaryList:public dictionary<KEY,VAL>
{  
public: dictionaryList();
		dictionaryList(VAL initVal);
	    int isEmpty()const;
		int includesKey(KEY k);
        VAL & operator[](KEY k);
        void removeKey(KEY k);
        void deleteAllValues();
        void setInitValue(VAL initVal);
        association<KEY,VAL>* associatedWith(KEY k);
		list<association<KEY,VAL>*>data;
		VAL initValue;
		friend class dictionaryListIterator<KEY,VAL>;
};

template <class KEY, class VAL> dictionaryList<KEY,VAL>::
dictionaryList():data(){}

template <class KEY, class VAL> dictionaryList<KEY,VAL>::
dictionaryList(VAL initVal):data(),initValue(initVal){}

template <class KEY, class VAL> int dictionaryList<KEY,VAL>::isEmpty()const
{ return data.isEmpty();}

template <class KEY, class VAL> void dictionaryList<KEY,VAL>::deleteAllValues()
{ data.deleteAllValues();}

template<class KEY,class VAL>association<KEY,VAL>*dictionaryList<KEY,VAL>::associatedWith(KEY k)
{ listIterator<association<KEY,VAL>*>itr(data);
  for (itr.init(); !itr; ++itr)
	if (itr()->key()==k) return itr();
  return 0;
}

template <class KEY, class VAL> int dictionaryList<KEY,VAL>::includesKey(KEY k)
{ return associatedWith(k)!=0;}

template <class KEY, class VAL> VAL& dictionaryList<KEY,VAL>::operator[](KEY k)
{ association<KEY,VAL>* newassoc=associatedWith(k);
  if (!newassoc) {
	newassoc= new association<KEY,VAL>(k,initValue); assert(newassoc !=0);
	data.add(newassoc);}
  return newassoc->valueField;
}

template <class KEY, class VAL> void dictionaryList<KEY,VAL>::removeKey(KEY k)
{ listIterator<association<KEY,VAL>*>itr(data);
  for (itr.init(); !itr; ++itr)
	  if (itr()->key()==k) { itr.removeCurrent(); break; }
}

template<class KEY,class VAL> void dictionaryList<KEY,VAL>::setInitValue(VAL initVal)
{initValue=initVal;}
//////////////////////////////////////////////////////////////
template<class KEY, class VAL> class dictionaryListIterator
   :public listIterator<association<KEY,VAL>*>
{public: dictionaryListIterator(dictionaryList<KEY,VAL>&dict);
};

template<class KEY, class VAL> dictionaryListIterator<KEY,VAL>::
dictionaryListIterator(dictionaryList<KEY,VAL>&dict)
:listIterator<association<KEY,VAL>*>(dict.data){}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -