我是否正确设置了此哈希 Table?
Am I Setting up this Hash Table Correctly?
我在数据结构中class,我们得到了一个关于哈希表的项目。我收到的说明摘录如下:
The hash table is an array of pointers to struct hash_table_entry
all initialized to Nil
.
所以这就是我写的(再次,整个程序的摘录):
hash_table_entry *hash_table = new hash_table_entry[hash_table_size];
for (int i=0;i<hash_table_size;i++)
{
hash_table[i] = new hash_table_entry;
}
hash_table_entry 是:
struct hash_table_entry{
char event_id; // Event id -- key used to hash on
int year; // Year of storm event
int event_index; // For the given year, the index into array of storm events };
所以我的问题是:
hash_table
是hash_table_entry
指针类型的数组,对吗?
- 当 for 循环遍历数组并创建新的
hash_table_entry
结构时,默认结构变量是否自动设置为“Nil
”?
提前感谢您的任何见解!
hash_table is an array of type hash_table_entry pointer, correct?
不,hash_table
是指向 hash_table_entry
的 指针,在使用 new hash_table_entry[hash_table_size];
进行给定初始化后,它将指向(第一个元素of) hash_table_entry
的 数组(不是指向 hash_table_entry
的 数组)。
When the for loop runs through the array and creates a new hash_table_entry struct, are the default struct variables automatically set to "Nil"?
如评论中所述,作业
hash_table[i] = new hash_table_entry;
无法编译,因为类型不匹配。 hash_table[i]
的类型为 hash_table_entry
,而 new hash_table_entry
的类型为 hash_table_entry*
.
但除此之外,对于初始化问题:new hash_table_entry
和 new hash_table_entry[hash_table_size]
(都创建对象或类型为 hash_table_entry
的对象数组)创建对象。因为这些表达式中没有给出初始值设定项,所以对象是默认构造的。因为 hash_table_entry
没有任何 user-declared/defined 构造函数,这意味着将使用隐式定义的默认构造函数。此构造函数确实执行 hash_table_entry
的所有成员的默认构造,这(因为它们是非 class 类型)意味着没有对它们进行任何操作,因此成员的值将保持 不确定初始化后。
这是否是 Nil
赋值的含义值得怀疑。 Nil
不是 C++ 中存在的东西,所以我假设 NULL
或 nullptr
是指并且你应该分配一个指针数组而不是对象并用空指针初始化它们.
指向 hash_table_entry
的指针数组分配有 new hash_table_entry*[hash_table_size]
,这导致 hash_table_entry**
,而不是 hash_table_entry*
。如果您添加 {}
,如:
new hash_table_entry*[hash_table_size]{}
或更明确地说
new hash_table_entry*[hash_table_size]{nullptr}
它还会为您将所有指针初始化为空指针,不需要循环。
我在数据结构中class,我们得到了一个关于哈希表的项目。我收到的说明摘录如下:
The hash table is an array of pointers to struct
hash_table_entry
all initialized toNil
.
所以这就是我写的(再次,整个程序的摘录):
hash_table_entry *hash_table = new hash_table_entry[hash_table_size];
for (int i=0;i<hash_table_size;i++)
{
hash_table[i] = new hash_table_entry;
}
hash_table_entry 是:
struct hash_table_entry{
char event_id; // Event id -- key used to hash on
int year; // Year of storm event
int event_index; // For the given year, the index into array of storm events };
所以我的问题是:
hash_table
是hash_table_entry
指针类型的数组,对吗?- 当 for 循环遍历数组并创建新的
hash_table_entry
结构时,默认结构变量是否自动设置为“Nil
”?
提前感谢您的任何见解!
hash_table is an array of type hash_table_entry pointer, correct?
不,hash_table
是指向 hash_table_entry
的 指针,在使用 new hash_table_entry[hash_table_size];
进行给定初始化后,它将指向(第一个元素of) hash_table_entry
的 数组(不是指向 hash_table_entry
的 数组)。
When the for loop runs through the array and creates a new hash_table_entry struct, are the default struct variables automatically set to "Nil"?
如评论中所述,作业
hash_table[i] = new hash_table_entry;
无法编译,因为类型不匹配。 hash_table[i]
的类型为 hash_table_entry
,而 new hash_table_entry
的类型为 hash_table_entry*
.
但除此之外,对于初始化问题:new hash_table_entry
和 new hash_table_entry[hash_table_size]
(都创建对象或类型为 hash_table_entry
的对象数组)创建对象。因为这些表达式中没有给出初始值设定项,所以对象是默认构造的。因为 hash_table_entry
没有任何 user-declared/defined 构造函数,这意味着将使用隐式定义的默认构造函数。此构造函数确实执行 hash_table_entry
的所有成员的默认构造,这(因为它们是非 class 类型)意味着没有对它们进行任何操作,因此成员的值将保持 不确定初始化后。
这是否是 Nil
赋值的含义值得怀疑。 Nil
不是 C++ 中存在的东西,所以我假设 NULL
或 nullptr
是指并且你应该分配一个指针数组而不是对象并用空指针初始化它们.
指向 hash_table_entry
的指针数组分配有 new hash_table_entry*[hash_table_size]
,这导致 hash_table_entry**
,而不是 hash_table_entry*
。如果您添加 {}
,如:
new hash_table_entry*[hash_table_size]{}
或更明确地说
new hash_table_entry*[hash_table_size]{nullptr}
它还会为您将所有指针初始化为空指针,不需要循环。