Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何调用模板函数来修改列表?_C++_Templates_Stdlist - Fatal编程技术网

C++ 如何调用模板函数来修改列表?

C++ 如何调用模板函数来修改列表?,c++,templates,stdlist,C++,Templates,Stdlist,我正在处理一些语法非常繁重的代码,但我认为我的代码不对 这是我的模板函数 template <typename T> int HashTable<T>::hash(const string& key) const { int temp = 0; for (int i = key.length(); i >= 0; i--) { temp = (13 * temp + key[i]) % NUM_BUCKETS; }

我正在处理一些语法非常繁重的代码,但我认为我的代码不对

这是我的模板函数

template <typename T>
int HashTable<T>::hash(const string& key) const {
    int temp = 0;
    for (int i = key.length(); i >= 0; i--) {
        temp = (13 * temp + key[i]) % NUM_BUCKETS;
    }
    return temp;
}
模板
int哈希表::哈希(常量字符串和键)常量{
内部温度=0;
对于(int i=key.length();i>=0;i--){
temp=(13*temp+键[i])%NUM_bucket;
}
返回温度;
}
这是我的名单

HashTable() { arr = new list <KeyValuePair<T>>[NUM_BUCKETS]; } 
HashTable(){arr=newlist[NUM_bucket];}
我正在尝试使用这个函数散列我的KeyValuePair,我是否已经接近正确地完成这个操作了

template <typename T>
void HashTable<T>::create(const string& key, const T& item) 
{
    HashTable key = hash(key->item).arr[NUM_BUCKETS].push_back(KeyValuePair<T>);

}
模板
void HashTable::create(常量字符串和键、常量T和项)
{
HashTable key=hash(key->item).arr[NUM\u bucket].push\u back(KeyValuePair);
}

in
HashTable key=hash(key->item)。arr[NUM\u bucket]。push\u back(KeyValuePair)
NUM_bucket
最有可能是计算出的哈希值。无论如何,
NUM_bucket
的索引超出了大小为
NUM_bucket
的数组的范围;您尝试访问键[i],它超出了范围,将其更改为:
int i=key.length()
,改为:
int i=key.length()-1
。这将有助于解决您的一个错误。