0

我正在写一个哈希类:

struct hashmap {
  void insert(const char* key, const char* value);
  char* search(const char* key);
 private:
  unsigned int hash(const char* s);
  hashnode* table_[SIZE]; // <--
};

由于 insert() 需要在插入新对时检查 table[i] 是否为空,因此我需要在启动时将表中的所有指针设置为 NULL。

我的问题是,这个指针数组table_会自动初始化为零,还是我应该在构造函数中手动使用循环将数组设置为零?

4

2 回答 2

6

The table_ array will be uninitialized in your current design, just like if you say int n;. However, you can value-initialize the array (and thus zero-initialize each member) in the constructor:

struct hash_map
{
    hash_map()
    : table_()
    {
    }

    // ...
};
于 2013-08-08T07:30:24.960 回答
0

您必须将所有指针设置为 NULL。您不必使用循环,您可以调用构造函数:

memset(table_, 0, SIZE*sizeof(hashnode*));
于 2013-08-08T07:28:13.247 回答