|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include<iostream>
- using namespace std;
- // pair 自带的容器
- template<class K,class E>
- class hashTable {
- public:
- hashTable(int theDiv=13,float theMaxFactor=0.8);
- int search(const K& theKey) const;
- pair<K,E>* find(const K& theKey) const;
- void insert(const pair<K,E>& thePair);
- int hash(const K& theKey);//用此函数代替
- void changeTable();//2*size+1 超过定义因子
- private:
- pair<K,E>** table;
- int dSize;
- int divisor;//也是默认桶的数量
- float MaxFactor;//用户指定
- //hash<K> hash;
- };
- template<class K,class E>
- hashTable<class K,class E>::hashTable(int theDiv,float theMaxFactor){
- divisor=theDiv;
- MaxFactor=theMaxFactor;
- table= new pair<K,E>* [divisor];
- for(int i=0;i<divisor;i++)
- table[i]= NULL;
- }
- template<class K,class E>
- int
- hashTable<class K,class E>::hash(const K& theKey){
- //默认就是整形 直接返回
- return theKey;
- }
- template<class K,class E>
- int
- hashTable<class K,class E>::search(const K& theKey) const{
- int i= hash(theKey) %divisor;
- int j=i;
- do{
- if(table[j]==NULL || table[j]->first== theKey)
- return j;
- j=(j+1)%divisor;
- }while(j!=i);
- return j;
- }
- template<class K,class E>
- std::pair<K,E>* //要加命名空间
- hashTable<class K,class E>::find(const K& theKey) const{
- int b=search(theKey);
- if(table[b]==NULL || table[b]->first != theKey)
- return NULL;
- return table[b];
- }
- template<class K,class E>
- void
- hashTable<class K,class E>::insert(const std::pair<K,E>& thePair){
- int b=search(thePair.first);
- if(table[b]==NULL) {
- table[b] = new pair<K,E>(thePair);
- dSize++;
- }
- else {
- if( table[b]->first == thePair.first;)
- table[b]->second= thePair.second;
- else
- cout<<"FULL!"<<endl;
- }
- if((float)dSize/divisor>MaxFactor)
- changeTable();
-
- }
- template<class K,class E>
- void
- hashTable<class K,class E>::changeTable(){//copy
- pair<K,E>** temp = pair<K,E> new[dSize*2+1];
- copy(table,table+dSize,temp);
- divisor= = dSize*2+1;
- delete table;
- table = temp;
- }
- int main(){
-
- return 0;
-
- }
复制代码
报错说(Template parameter list matching the non-templated nested type 'hashTable<K, E>' should be empty ('template<>')
怎么解决呢
|
|