【问题标题】:Why do I need double pointers for an array of objects? HashMap Example in C++为什么我需要一个对象数组的双指针? C++ 中的 HashMap 示例
【发布时间】:2017-08-02 18:49:51
【问题描述】:

我正在练习一些 C++,我很困惑为什么我需要为对象数组(例如节点结构)使用双指针。这是一个简单的代码sn-p来解释我的情况:

struct HashNode{

    HashNode* next;
    int data;
    int key;
    int hashCode;

    HashNode::HashNode(
        HashNode* next, 
        const int& data, 
        const int& key, 
        const int& hashCode
        ) : next(next), data(data), key(key), hashCode(hashCode)
        {}

};

class HashMap{
    public:
        HashMap();
        HashMap(int tableSize);
        ~HashMap();

    private:
        //Here is the double pointer
        HashNode** table;
};

HashMap::HashMap(){
    //Here is the array initialization 
    table = new HashNode*[100];
}

我已经删除了问题不需要的代码。

如果我这样删除双指针:

HashNode* table;

table = new HashNode[100];

我收到以下错误。

hashmap.cpp: In method `HashMap::HashMap()':
hashmap.cpp:87: no matching function for call to `HashNode::HashNode ()'
hashmap.cpp:61: candidates are: HashNode::HashNode(const HashNode &)
hashmap.cpp:58:                 HashNode::HashNode(HashNode *, const int &, cons
t int &, const int &)

这表明 HashNode 尝试运行构造函数。

如果我只将数组的初始化更改为table = new HashNode*[100];,同时保留HashNode* table;,则会出现以下错误。

hashmap.cpp: In method `HashMap::HashMap()':
hashmap.cpp:87: assignment to `HashNode *' from `HashNode **'

我的假设是,当我创建一个对象数组时,我需要对象的生命周期与程序的持续时间一样。这需要我对对象和数组使用指针。因此,我需要数组的双指针,因为它指向指针,我需要对象的指针。

但是,我仍然不确定,我在网上找不到任何好的解释。有人可以解释一下这种情况吗?

【问题讨论】:

  • 您需要阅读good C++ book
  • 在这里问这个问题不好吗?我认为这是一个很好的例子,但我没有掌握这个概念。我会看书谢谢!
  • format这个问题还不错,只是琐碎而已。有一个特殊要求没有得到满足:在询问之前尽可能多地进行研究

标签: c++ pointers double-pointer


【解决方案1】:

此实现使用separate chaining with linked lists 来管理哈希冲突。因此,table 是一个指向HashNode 的指针数组,这意味着它需要两个星号:

  • 一个星号来自数组元素的类型,即HashNode*
  • 另一个星号来自HashNode*的数组

这也是为什么new 表达式中有一个星号:

table = new HashNode*[100];
//                  ^

【讨论】:

    【解决方案2】:

    看来您对 c++ 指针很陌生。 您目前正在做的是制作 100 个指针的数组。所以编译器不会给你任何错误,因为实际对象不是用这一行创建的。 HashNode **table = new HashNode*[100];

    但是当你使用 HashNode *table = new HashNode[100]; 然后您尝试为 HashNode 创建 100 个对象; 但是您没有默认构造函数,因此编译器会给您上述错误。

    我附上了以下工作代码。看看吧。

        #include <iostream>
        using namespace std;
    
        struct HashNode{
    
            HashNode* next;
            int data;
            int key;
            int hashCode;
    
            HashNode(){}
    
            HashNode(
                HashNode* next, 
                const int& data, 
                const int& key, 
                const int& hashCode
                ) : next(next), data(data), key(key), hashCode(hashCode)
                {}
    
        };
    
        class HashMap{
            public:
                HashMap();
    
            private:
                //Here is the double pointer
                HashNode* table;
        };
    
        HashMap::HashMap(){
            //Here is the array initialization 
            table = new HashNode[100];
        }
    
        int main() {
            // your code goes here
    
            HashMap ob;
            std::cout << "him" <<  std::endl;
            return 0;
        }
    

    【讨论】:

      【解决方案3】:

      在这里你要声明指针数组

      HashNode** 表;

      这是一个名为 table 的数组,其指针类型为 hashNode。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-01-26
        • 1970-01-01
        • 2010-10-28
        • 2010-12-11
        • 1970-01-01
        • 2018-10-08
        • 1970-01-01
        相关资源
        最近更新 更多