在类中创建动态数组(c++)

Create a dynamic array within a class (C++)

本文关键字:c++ 数组 动态 创建      更新时间:2023-10-16

我想实现一个类与数组。如果超出了数组的大小,我会调用resize()函数。然而,我似乎不能编码一个动态数组作为数据成员。有人能指导我如何解决这个问题吗?

这是我目前得到的:

class ArrayList
{
        public:
        ArrayList()
        {
                array[ARR_SIZE] = {0};
                space = 0;
                size = ARR_SIZE;
        }
        void insert(int place, int value)
        {
                if (place >= size)
                        cout << "Sorry, that index is not accessible" << endl;
                else
                {
                        array[place] = value;
                        space++;
                if(space == size)
                        allocate();
                }
        }
        void remove(int place)
        {
                if(place >= size)
                        cout << "Sorry, that index is not accessible" << endl;
                else
                {
                        array[place] = NULL;
                        space--;
                }
        }
        void allocate()
        {
                int* array = new int[size*2];
                size = size*2;
        }
        int usedSize()
        {
                return space;
        }
        int totalSize()
        {
                return size;
        }
        private:
        int array[ARR_SIZE];
        int space;
        int size;
};

使用std::vector。c++不支持可变长度或动态大小的数组,无论是在类中还是在其他地方。

使用std::vector并让它为您管理一切:

#include <vector>
class ArrayList
{
public:
    ArrayList()
    {
        array.reserve(ARR_SIZE);
    }
    void insert(int place, int value)
    {
        if ((place < 0) || (place > array.size()))
            std::cout << "Sorry, that index is not accessible" << std::endl;
        else
            array.insert(array.begin()+place, value);
    }
    void remove(int place)
    {
        if ((place < 0) || (place >= array.size()))
            std::cout << "Sorry, that index is not accessible" << std::endl;
        else
            array.erase(array.begin()+place);
    }
    int usedSize()
    {
        return array.size();
    }
    int totalSize()
    {
        return array.capacity();
    }
private:
    std::vector<int> array;
};

如果你真的想自己管理数组内存,那你就大错特错了。试试这个:

class ArrayList
{
public:
    ArrayList()
    {
        array = new int[ARR_SIZE];
        space = 0;
        size = ARR_SIZE;
    }
    ArrayList(const ArrayList &src)
    {
        array = new int[src.size];
        space = src.space;
        size = src.size;
        for(int i = 0; i < space; ++i)
            array[i] = src.array[i]; 
    }
    ~ArrayList()
    {
        delete[] array;
    }
    void insert(int place, int value)
    {
        if ((place < 0) || (place > space))
            std::cout << "Sorry, that index is not accessible" << std::endl;
        else
        {
            if (space == size)
                allocate();
            for(int i = space-1; i > place; --i)
                array[i] = array[i-1];
            array[place] = value;
            ++space;
        }
    }
    void remove(int place)
    {
        if ((place < 0) || (place >= space))
            std::cout << "Sorry, that index is not accessible" << std::endl;
        else
        {
            for(int i = place+1; i < space; ++i)
                array[i-1] = array[i];
            --space;
        }
    }
    void allocate()
    {
        int* newarray = new int[size*2];
        for (int i = 0; i < space; ++i)
            newarray[i] = array[i];
        delete[] array;
        array = newarray;
        size *= 2;
    }
    int usedSize()
    {
        return space;
    }
    int totalSize()
    {
        return size;
    }
    ArrayList& operator=(const ArrayList &src)
    {
        int *newarray = new int[src.size];
        for(int i = 0; i < src.space; ++i)
            newarray[i] = src.array[i]; 
        delete[] array;
        array = newarray;
        space = src.space;
        size = src.size;
        return *this;
    }
private:
    int *array;
    int space;
    int size;
};

我还没有声誉给你留下评论,但是你试过c++的std::vector吗?它做的正是你要找的:

http://www.cplusplus.com/reference/vector/vector/?kw=vector

听起来应该使用malloc和realloc。这应该和Remy使用new和delete的答案一样。

将缓冲区声明为int*类型,并通过递增的变量跟踪缓冲区的大小。如果您的大小超过动态数组中的空间量,请重新分配。