我正在写一个哈希表,但我遇到了一个困难。我想用标准容器(向量、列表等)的内容来初始化它,比如地图:
map <string,int> m(a.begin(),a.end())
我有以下类定义:template <class key,class val,class hashik=std_hash> class hash_table
.
我定义了一个构造函数:
template <template <class> class C> hash_table(typename C<pair <key,val> >::iterator first,typename C<pair <key,val> >::iterator last)
{
init();
for(pair <key,val>* it=first;it!=last;++it)
this->operator[](it->first)=it->second;
}
但它不编译。呼叫没有匹配的功能。例如:
vector <pair <string,int> > a;
...
hash_table <string,int> m(a.begin(),a.end()); //compilation error
我究竟做错了什么?您可以建议我阅读哪些有关模板的书籍?