hash表实现小问题,谢谢!
这个问题真不知道怎么解决了,只要编译通过就行了,很感谢!
(main()都写成那样了,还报错,
一直会报错:copy.h:63: 错误:expected initializer before ‘ <’ token
copy.h:77: 错误:expected initializer before ‘ <’ token
#include <list>
#include <iostream>
#include <string>
using namespace std;
template <class key,class T>
struct value_type
{
public:
key first;
Tsecond;
value_type(const key& key1,const T& t):first(key1)
{
second = t;
}
bool operator == (const value_type & x)
{
return first == x.first;
}
};//struct value_type
template <class key,class T,class HashFunc> class hash_map{
typedefkeykey_type;
typedefHashFunchash_func;
list < value_type <const key_type,T> > *buckets;
const int DEFAULTL_SIZE;
int count,
length;
hash_funchash;
public:
classiterator;
hash_map():DEFAULTL_SIZE(211)
{
count = 0;
length = DEFAULTL_SIZE;// = 211
buckets = new list <value_type <const key_type,T> > [DEFAULTL_SIZE];
}
T&operator[](const key_type& key);
iteratorfind(const key_type& key);
~hash_map();
classiterator{
public:
unsigned index;
unsigned length;
typename list < value_type <const key_type,T> > ::iteratorlist_itr;
list < value_type <const key_type,T> > *buckets;
iterator(){}
booloperator == (const iterator& other)const
{
return (index == other.index) && (list_itr == other.list_itr)
&& (buckets == other.buckets) && (length == other.length);
}// ==
value_type <const key_type,T> & operator*()
{
return *list_itr;
}
};//class iterator
};
template <class key,class T,class HashFunc> typename hash_map <key,T,HashFunc> ::iterator hasp_map <key,T,HashFunc> ::find(const key_type & key)
{
int index = hash(key) %length;
iterator itr;
itr.list_itr = std::find(buckets[index].begin(),buckets[index].end(),value_type <const key_type,T> (key,T()));
if(itr.list_itr == buckets[index].end())
return end();
itr.buckets = buckets;
itr.index = index;
itr.length = length;
return itr;
}//find
//取值
template <class key,class T,class HashFunc> T& hasp_map <key,T,HashFunc> ::operator[](const key_type& key)
{
return ( *((insert(value_type <const key_type,T> (key,T()))).first).second);
}//operator[]
classhash_func
{
public:
unsignedlongoperator()(const int &key)
{
return(unsigned long)key;
}
};
int main()
{
return 0;
}
[解决办法]
你这个地方
template <class key,class T,class HashFunc>
class hash_map {
// 是不是应该public一下 ?
public:
typedef key key_type;
typedef HashFunc hash_func;
.........
};
[解决办法]
哎, hasp_map简单的拼写错误。
[解决办法]
http://www.cl.cam.ac.uk/~cwc22/hashtable/