我的HashMap
函数似乎存在一些模板问题。我需要最后运行一个<string, vector<string>>
哈希映射。它提出了很多问题。
我需要能够在向量上推送项目。我正在使用[]运算符。
测试
using namespace std;
#include "MyHashMap.hpp"
#include <vector>
int main()
{
MyHashMap <string, vector <string> > test (10);
test["x"].push_back("Y");
}
错误(其中有很多。我认为它们是相关的所以我只会显示1)
在成员函数'valueType&amp; MyHashMap :: operator [with keyType = std :: basic_string,std :: allocator&gt;,valueType = std :: vector,std :: allocator&gt;,std :: allocator,std :: allocator&gt; &GT; &GT;]”:
test.cpp:13:从这里实例化
MyHashMap.hpp:52:错误:无法转换'MyHashMap,std :: allocator&gt;,std :: vector,std :: allocator&gt;,std :: allocator,std :: allocator&gt; &GT; &GT;分配中的&gt; :: EntryType'到'EntryType' MyHashMap.hpp:在成员函数'int MyHashMap :: findPos(keyType&amp;)[with keyType = std :: basic_string,std :: allocator&gt;,valueType = std :: vector,std :: allocator&gt;,std :: allocator,std :: allocator&gt; &GT; &GT;]”:
MyHashMap.hpp:46:从'valueType&amp;中实例化MyHashMap :: operator [with keyType = std :: basic_string,std :: allocator&gt;,valueType = std :: vector,std :: allocator&gt;,std :: allocator,std :: allocator&gt; &GT; &GT;]”
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
哈希条目
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
答案 0 :(得分:2)
您的会员MyHashMap::array
的声明是错误的。将其更改为:
HashEntry <keyType, valueType>* array;
在构造函数中:
explicit MyHashMap(int size = 1000) : tableSize(size)
{
array = new HashEntry<keyType, valueType>[tableSize];
}
它应该可以正常工作!