我知道*定义一个指针... **确定一个指向指针的指针?
如果是这样,为什么?
指针指针有时被称为引用吗?只需要澄清以下非常简单的哈希.
通常,当传递整个内容时,指针用于传递较大结构的位置.
我已经看到指向用于在quantlib项目中使用的指针的指针来创建"句柄",因为每个"观察者"都拥有一个指向"术语结构"指针的指针,该指针可能在运行时发生变化,因此指针保持另一个指针的位置.
但是我看到这里没有相关性?
class hash_entry
{
private:
int key;
int value;
public:
hash_entry(int key, int value)
{
this->key = key;
this->value = value;
}
int getKey()
{
return key;
}
int getValue()
{
return value;
}
};
class hash_map
{
private:
hash_entry **table;
static const int TABLE_SIZE = 128;
public:
hash_map()
{
table = new hash_entry*[TABLE_SIZE];
for (int i = 0; i < TABLE_SIZE; i++)
table[i] = NULL;
}
int get(int key)
{
int hash = (key % TABLE_SIZE);
while (table[hash] != NULL && table[hash]->getKey() != key)
hash = (hash + 1) % TABLE_SIZE;
if (table[hash] == NULL)
return -1;
else
return table[hash]->getValue();
}
void put(int key, int value)
{
int hash = (key % TABLE_SIZE);
while (table[hash] != NULL && table[hash]->getKey() != key)
hash = (hash + 1) % TABLE_SIZE;
if (table[hash] != NULL)
delete table[hash];
table[hash] = new hash_entry(key, value);
}
~hash_map()
{
for (int i = 0; i < TABLE_SIZE; i++)
if (table[i] != NULL)
delete table[i];
delete[] table;
}
};
Run Code Online (Sandbox Code Playgroud)
是的,**定义指向指针的指针(因为规范是这样说的).不,我无法想象有人称这是一个参考.
至于为什么他们在这种情况下使用它,他们正在编写(非常类似C)代码来动态分配指向X的指针数组.这段代码:
hash_entry **table;
[ ... ]
hash_map() {
table = new hash_entry*[TABLE_SIZE];
Run Code Online (Sandbox Code Playgroud)
是大致相当于:
std::vector<hash_entry *> table(TABLE_SIZE);
Run Code Online (Sandbox Code Playgroud)
(虽然目前,我还没有把它分开,因为你需要为一个班级成员).
| 归档时间: |
|
| 查看次数: |
167 次 |
| 最近记录: |