【发布时间】:2015-02-26 21:29:20
【问题描述】:
我正在用 C++ 创建一个哈希表,但我的 C++ 有点生疏。 在预定义哈希表的大小时,我可以很好地编写所有代码,但是,我希望用户能够确定他们希望哈希表有多大。
目前在我的 Hash.h 文件的私有部分中,我有以下代码..
// static const int tableSize = 10;
static const int tableSize = 100;
//Types of things that the item consists of
struct item {
string name;
string drink;
item* next; //Point to next item in the hash table
};
item* HashTable[tableSize];
};
我的目标是让 tableSize 变量成为用户输入的整数。这样做的最佳方法是什么?
【问题讨论】:
-
使用
std::vector<item *>。 -
如果你使用
std::list或std::vector,你可以放下next指针,因为顺序基本上是由列表或向量中的位置来处理的。 -
是否不能让用户输入一些可变整数值并将其分配给他们想要使用的表大小?这就是它应该制作的方式。