hashnode构造函数如何初始化
How the hashnode constructor gets Initialized
我正在尝试使用单独的链接来实现哈希。虽然我们只是为类hashmap
构造对象,但我无法理解hashnode
构造函数是如何初始化的。它们彼此都是私有的,而且我还没有从hashnode
派生类hashmap
。那么这是怎么回事呢?
#include <iostream>
using namespace std;
static const int table_size = 10;
class hashnode {
public:
int key;
int value;
hashnode* next;
hashnode(int key, int value)
{
this->key = key;
this->value = value;
this->next = NULL;
}
};
class hashmap {
hashnode** table;
public:
hashmap()
{
table = new hashnode* [table_size];
for(int i=0;i<table_size;i++)
{
table[i] = NULL;
}
}
int hash(int key)
{
return key%table_size;
}
void insert(int key, int value)
{
int hash_val = hash(key);
hashnode* prev = NULL;
hashnode* entry = table[hash_val];
while(entry != NULL)
{
prev = entry;
entry = entry->next;
}
if(entry == NULL)
{
entry = new hashnode(key,value);
if(prev == NULL)
{
table[hash_val] = entry;
}
else
{
prev->next = entry;
}
}
else
{
entry->value = value;
}
}
void printtable(int key)
{
while(table[0] != NULL)
{
cout<<table[0]->key<<endl;
table[0] = table[0]->next;
}
}
};
int main()
{
hashmap hash1;
int key,value;
int choice;
行table = new hashnode* [table_size];
正在调用hashnode
的构造函数。new
就是这么做的;它为每个对象分配空间,然后调用对象的构造函数。
您要求编译器创建10个(基于table_size
)hashnode
对象。
相关文章:
- 为什么std::vector和std::valarray初始化构造函数不同
- 初始化构造函数C++中结构的向量
- C++ 中常量属性的初始化构造函数错误
- 不正确的输出和变量未用Eclipse CDT初始化构造函数
- 如何使用嵌套初始化构造函数中的一维向量初始化矩阵
- 如何通过参数初始化构造函数中的数组?
- 是否有理由使用 malloc 初始化构造函数中的指针
- 如何在 c++ 中初始化构造函数中的二维数组
- 为什么初始化构造函数列表参数时会发生异常?
- 无法初始化构造函数
- 初始化构造函数的默认参数的优选方法是什么?
- 在C++17中使用空列表初始化构造函数时发生编译错误
- C++ 初始化构造函数初始化列表中的嵌套结构?
- 初始化构造函数c++中的向量
- g++ (GCC) 4.6.0 我有以下类,我正在尝试初始化构造函数的结构成员初始化列表
- 使用字符串文本初始化构造函数中的 std::array<char,x> 成员。海湾合作委员会错误?
- 初始化构造函数C++中函数的成员指针
- 在c++中初始化构造函数中的静态成员变量时出错
- memset()初始化构造函数中的对象
- 初始化构造函数C++中的成员向量