c++ - 哈希表的复制构造函数

标签 c++ copy hashtable

我在为哈希表编写复制构造函数时遇到了问题。我已经在单独使用链接对象的复制构造函数方面遇到了麻烦。如何使用以下函数实现复制构造函数和赋值运算符:

//copy constructor
Apple::Apple(const Apple& anotherApple){


}

//assignment operator
Apple& Apple::operator = (const Apple& anotherApple){


}

我知道它会是这样的:

tableSize = anotherApple.tableSize;
count = anotherApple.count;
array = new *anotherApple.array; //idk if this is right for dynamically created array of pointers

最佳答案

Apple::Apple(Apple const & anotherApple)
{
     // allocate an array to hold pointers to Node
     array = new Node*[tableSize];

     for(int i=0; i<tableSize; i++)
     {
        Node* src = anotherApple.array[i];
        Node* dest;

        if (src == NULL)
        {
           array[i] = NULL;
           continue; // no data to copy, continue to next row
        }


        // set array[i] since there is at-least one element
        dest = new Node;
        dest->data = src->data;

        array[i] = dest;
        src = src->next;

        while(src != NULL)
        {
           Node* n = new Node;
           dest->next = n;
           dest = n;

           dest->data = src->data;

           src = src->next;
        }

        dest->next = NULL;
     }
  }

关于c++ - 哈希表的复制构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34487621/

相关文章:

c++ - 屏幕休眠时的 AFX_WM_DRAW2D 消息

c++ - 缓存 C/C++ 程序中的值的命中/未命中

java - 为什么这个 JNI 程序不将浮点值复制回 Java 端?

c++ - 使哈希表成为用户输入的大小

java - 如何根据用户输入在哈希表中添加某些值?

c++ - 析构函数被调用

c++ - Netbeans 代码完成在 C++ 中过于频繁地弹出

php 并将图像移动到新文件夹

c# - 从递归最小的目录中复制文件

python - 无法正确按字谜分组