C++ C++;模板化HashMap实现错误
我的C++ C++;模板化HashMap实现错误,c++,compiler-errors,hashmap,implementation,C++,Compiler Errors,Hashmap,Implementation,我的HashMap函数似乎有一些模板问题。最后我需要运行一个哈希映射。这给了我们很多问题 我需要能够在向量上推送项目。我正在使用[]操作符 试验 使用名称空间std; #包括“MyHashMap.hpp” #包括 int main() { MyHashMap测试(10); 测试[“x”]。推回(“Y”); } 错误(有很多错误。我认为它们是相关的,所以我只显示1) 在成员函数“valueType&MyHashMap::operator[with keyType=std::basic_字符串,s
HashMap
函数似乎有一些模板问题。最后我需要运行一个
哈希映射。这给了我们很多问题
我需要能够在向量上推送项目。我正在使用[]操作符
试验
使用名称空间std;
#包括“MyHashMap.hpp”
#包括
int main()
{
MyHashMap测试(10);
测试[“x”]。推回(“Y”);
}
错误(有很多错误。我认为它们是相关的,所以我只显示1)
在成员函数“valueType&MyHashMap::operator[with keyType=std::basic_字符串,std::allocator>,valueType=std::vector,std::allocator>,std::allocator,std::allocator>>]”中:
test.cpp:13:从此处实例化
MyHashMap.hpp:52:错误:无法将分配中的“MyHashMap,std::allocator>,std::vector,std::allocator>,std::allocator>,std::allocator>>>>::EntryType”转换为“EntryType”
MyHashMap.hpp:在成员函数“int MyHashMap::findPos(keyType&)[带keyType=std::basic_字符串,std::allocator>,valueType=std::vector,std::allocator>,std::allocator,std::allocator>>]”中:
hpp:46:从“valueType&MyHashMap::operator[带keyType=std::basic_字符串,std::allocator>,valueType=std::vector,std::allocator>,std::allocator,std::allocator>>”实例化”
MyHashMap.hpp
#include <string>
#include "HashEntry.hpp"
using namespace std;
template <typename keyType, typename valueType>
class MyHashMap
{
public:
/***********************************************
explicit HashTable( int size = 1009 ) : array(size)
CONSTRUCTOR with default size of 1009
***********************************************/
explicit MyHashMap(int size=1000):tableSize(size)
{
array = new HashEntry <keyType, valueType> [tableSize];
}
int size()
{
return tableSize;
}
/***********************************************
***********************************************/
valueType& operator[](keyType x )
{
// Insert x as active
int currentPos = findPos( x );
if( isActive( currentPos ) )
{
return array[currentPos].value;
}
array[ currentPos ].key = x;
array[ currentPos ].info = ACTIVE;
return array[currentPos].value;
}
/***********************************************
Returns pointer to HashEntry if it is active
Returns NULL otherwise
***********************************************/
HashEntry<keyType,valueType>& getPos(int n)
{
if(isActive(n))
{
return array[n];
}
}
/***********************************************
bool insert( keyType & x, & valueType y)
inserts x and y into hashMap
***********************************************/
bool insert( keyType & x, valueType& y)
{
// Insert x as active
//currentPos gets position to try to insert
int currentPos = findPos( x );
if( isActive( currentPos ) )
{
return false;
}
array[ currentPos ].key = x;
array[ currentPos ].value = y;
return true;
}
/***********************************************
LAZY DELETION
***********************************************/
bool remove( keyType x )
{
int currentPos = findPos( x );
if( !isActive( currentPos ) )
return false;
array[ currentPos ].info = DELETED;
return true;
}
/***********************************************
***********************************************/
valueType* find( keyType & x )
{
//currentPos gets position where Search for x terminates
int currentPos = findPos(x);
if(isActive(currentPos))
return array[currentPos].value;
else
return NULL;
}
enum EntryType { ACTIVE, EMPTY, DELETED };
private:
HashEntry <keyType, valueType>* array;
int tableSize;
/***********************************************
***********************************************/
bool isActive( int currentPos )
{
return array[ currentPos ].info == ACTIVE;
}
/***********************************************
int findPos( HashedObj & x )
Returns the position where the search for x terminates
***********************************************/
int findPos( keyType & x )
{
int offset = 1;
int currentPos = myhash( x );
// Assuming table is half-empty, and table length is prime,
// this loop terminates
while( array[ currentPos ].info != EMPTY &&
array[ currentPos ].element != x )
{
currentPos += offset; // Compute ith probe
offset += 2;
if( currentPos >= tableSize )
currentPos -= tableSize;
}
return currentPos;
}
/***********************************************
Hash function for string
***********************************************/
int myhash( string & x )
{
int hashVal = 0;
for(int i = 0; i < x.length(); i++)
{
hashVal = 37*hashVal+x[i];
}
hashVal %= tableSize;
if(hashVal < 0)
hashVal += tableSize;
return hashVal;
}
};
#endif
#包括
#包括“HashEntry.hpp”
使用名称空间std;
模板
类MyHashMap
{
公众:
/***********************************************
显式哈希表(int size=1009):数组(size)
默认大小为1009的构造函数
***********************************************/
显式MyHashMap(int size=1000):表大小(size)
{
数组=新的HashEntry[表大小];
}
int size()
{
返回表大小;
}
/***********************************************
***********************************************/
valueType和运算符[](键类型x)
{
//将x插入为活动状态
int currentPos=findPos(x);
如果(isActive(currentPos))
{
返回数组[currentPos]。值;
}
数组[currentPos].key=x;
数组[currentPos].info=活动;
返回数组[currentPos]。值;
}
/***********************************************
如果HashEntry处于活动状态,则返回指向它的指针
否则返回NULL
***********************************************/
HashEntry和getPos(int n)
{
如果(i活动(n))
{
返回数组[n];
}
}
/***********************************************
布尔插入(键类型和x、值类型和y)
将x和y插入hashMap
***********************************************/
布尔插入(键类型和x、值类型和y)
{
//将x插入为活动状态
//currentPos获取要尝试插入的位置
int currentPos=findPos(x);
如果(isActive(currentPos))
{
返回false;
}
数组[currentPos].key=x;
数组[currentPos]。值=y;
返回true;
}
/***********************************************
惰性删除
***********************************************/
bool拆卸(钥匙类型x)
{
int currentPos=findPos(x);
如果(!isActive(currentPos))
返回false;
数组[currentPos].info=已删除;
返回true;
}
/***********************************************
***********************************************/
valueType*查找(键类型和x)
{
//currentPos获取搜索x终止的位置
int currentPos=findPos(x);
如果(isActive(currentPos))
返回数组[currentPos]。值;
其他的
返回NULL;
}
枚举入口类型{活动,空,已删除};
私人:
HashEntry*数组;
int表大小;
/***********************************************
***********************************************/
布尔isActive(int currentPos)
{
返回数组[currentPos].info==活动;
}
/***********************************************
int findPos(HashedObj&x)
返回搜索x终止的位置
***********************************************/
int findPos(键类型和x)
{
int offset=1;
int currentPos=myhash(x);
//假设表为半空,表长度为素数,
//此循环终止
while(数组[currentPos].info!=空&&
数组[currentPos]。元素!=x)
{
currentPos+=偏移量;//计算第i个探针
偏移量+=2;
如果(currentPos>=表大小)
currentPos-=表大小;
}
返回当前位置;
}
/***********************************************
字符串的哈希函数
***********************************************/
int myhash(字符串和x)
{
int hashVal=0;
对于(int i=0;i
散列条目
using namespace std;
enum EntryType { ACTIVE, EMPTY, DELETED };
template <typename keyType, typename valueType>
struct HashEntry
{
keyType key;
valueType value;
EntryType info;
/***********************************************
HashEntry CONSTRUCTOR
***********************************************/
HashEntry(keyType x = keyType(), valueType y = valueType(),
EntryType i = EMPTY ):key(x), value(y),info( i ) { }
};
#endif
使用名称空间std;
枚举入口类型{活动,空,已删除};
模板
结构哈希项
{
键型键;
值类型值;
入口类型信息;
/***********************************************
HashEntry构造函数
***********************************************/
HashEntry(keyType x=keyType(),valueType y=valueType(),
EntryType i=EMPTY):键(x)、值(y)、信息(i){
};
#恩迪夫
您的成员的声明MyHashMap::array
是错误的。将其更改为:
HashEntry*数组代码>
在押
using namespace std;
enum EntryType { ACTIVE, EMPTY, DELETED };
template <typename keyType, typename valueType>
struct HashEntry
{
keyType key;
valueType value;
EntryType info;
/***********************************************
HashEntry CONSTRUCTOR
***********************************************/
HashEntry(keyType x = keyType(), valueType y = valueType(),
EntryType i = EMPTY ):key(x), value(y),info( i ) { }
};
#endif
explicit MyHashMap(int size = 1000) : tableSize(size)
{
array = new HashEntry<keyType, valueType>[tableSize];
}