【发布时间】:2021-02-17 14:41:21
【问题描述】:
我目前正在使用私有数据成员 vector<list<pair<K, V>>> hashTable; 制作哈希表。
我需要访问每个列表,进而访问每个列表以实现各种不同的功能。我目前正在这样做:
for(int i = 0; i < hashTable.capacity(); i++){
list<pair<K,V>>* listPtr = hashTable[i];
for(pair<K,V>* pairPtr = listPtr->front(); pairPtr != listPtr->end(); pairPtr++){
pair<K,V> tempPair;
tempPair.first = pairPtr->first;
tempPair.second = pairPtr->second;
insert(tempPair);
}
}
}
上面的代码是我的 rehash 函数的一部分。 Insert 插入基于散列函数的对,散列函数基于向量的大小进行散列。这并不重要。我只想知道如何到达每个列表,然后是每一对。
我的问题是,有没有更好的方法来访问向量中的每个列表和对?
【问题讨论】:
-
首先应该是
hashTable.size()。对于向量,容量与大小不同。 -
list<pair<K,V>>* listPtr = hashTable[i];无法编译。也许你的意思是list<pair<K,V>>* listPtr = hashTable + i;? -
@john 抱歉,我的意思是将 tempList 键入为 hashTable。那是来自不同功能的 sn-p。你是对的,我应该使用尺寸。我刚刚尝试编译我的代码并看到了这一点。那么,我如何访问列表并因此配对呢?
-
同上
pair<K,V>* pairPtr = listPtr->front()无法编译。 -
你为什么要将每一对复制到一个临时文件中,然后将它插入到某个东西中?