関数のテンプレート化に問題があるようです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& MyHashMap::operator [with keyType = std::basic_string, 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&) [with keyType = std::basic_string, std::allocator >, valueType = std::vector, std::allocator >, std::アロケータ、std::アロケータ > > >]':
MyHashMap.hpp:46: 'valueType& MyHashMap::operator からインスタンス化 [with keyType = std::basic_string, std::allocator >, valueType = std::vector, std::allocator >, std::allocator, std::アロケータ > > >]'</p>
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