简体   繁体   English

C++ 中的动态数组实现

[英]Dynamic Array implementation in C++

I am trying to implement Dynamic Array using C++.我正在尝试使用 C++ 实现动态数组。 However, my resize() function seem to not work properly.但是,我的resize() function 似乎无法正常工作。 There are no errors or warnings.没有错误或警告。 I did some research and tried to look at other implementations found on the internet but was unable to fix the issue.我做了一些研究并试图查看在互联网上找到的其他实现,但无法解决问题。 I put my code below.我把我的代码放在下面。

#include <iostream>

class Array
{
private:
    int* arr;
    int size = 0;
    int capacity = 1;

public:
    Array() { arr = new int[capacity]; }

    Array(int capacity)
        :
        capacity(capacity)
    {
        arr = new int[capacity];
    }

    int length() const { return size; }

    bool is_empty() const { return (length() == 0); }

    int get(int index) const { return arr[index]; }

    void set(int index, int value) { arr[index] = value; }

    void resize()
    {
        capacity *= 2;
        int* temp = new int[capacity];
        for (int i = 0; i < size; i++) { temp[i] = arr[i]; }
        delete[] arr;
        arr = temp;
        for (int i = 0; i < capacity; i++) { arr[i] = 0; }
    }

    void add(int value)
    {
        if (size + 1 >= capacity) { resize(); }
        arr[size++] = value;
    }

    void remove(int index)
    {
        for (int i = index; i < size - 1; i++)
        {
            arr[i] = arr[i + 1];
        }
        size--;
    }

    int& operator[](int index)
    {
        return arr[index];
    }
};

int main()
{
    Array array;

    for (int i = 0; i < 5; i++)
    {
        array.add(i + 1);
    }

    for (int i = 0; i < array.length(); i++)
    {
        std::cout << array.get(i) << " ";
    }
    std::cout << '\t' << array.length() << '\n';

    return 0;
}

The code outputs:代码输出:

0 0 0 4 5   5

But I expect it to output:但我希望它是 output:

1 2 3 4 5   5

In your resize method you copied over the existing elements from arr在您的resize方法中,您从arr复制了现有元素

for (int i = 0; i < size; i++) { temp[i] = arr[i]; }

But then later you 0 all of the elements out, effectively clearing the previous data但是后来你把所有的元素都清0了,有效的清除了之前的数据

for (int i = 0; i < capacity; i++) { arr[i] = 0; }

Instead you likely just want to 0 the trailing new elements相反,您可能只想将尾随的新元素设为0

for (int i = size; i < capacity; ++i) { arr[i] = 0; }

This loop这个循环

for (int i = 0; i < capacity; i++) { arr[i] = 0; }

is incorrect.是不正确的。 It sets the first size elements to 0.它将第一个 size 元素设置为 0。

In fact this loop is redundant.事实上,这个循环是多余的。 Instead of this loop you could just write in the statement where the memory is allocated like而不是这个循环,你可以写在 memory 被分配的语句中

int* temp = new int[capacity]();

Also the definition of the function add() is incorrect. function add()的定义也不正确。 It should look like:它应该看起来像:

void add(int value)
{
    if (size == capacity) { resize(); }
    arr[size++] = value;
}

You need to define explicitly the destructor.您需要明确定义析构函数。 For example例如

~Array()
{
    delete []arr;
}

Also you need either to define explicitly a copy constructor and the copy assignment operator or define them as deleted.您还需要明确定义复制构造函数和复制赋值运算符或将它们定义为已删除。 For example例如

Array( const Array & ) = delete;
Array & operator =( const Array & ) = delete;

Otherwise using these member functions can result in undefined behavior.否则,使用这些成员函数可能会导致未定义的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM