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

Dar*_*i99 0 c++ class arraylist

我想用数组实现一个类.如果数组的大小被破坏,我会调用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;
};
Run Code Online (Sandbox Code Playgroud)

Rob*_*ahy 7

使用std::vector.C++不支持类或其他类型的可变长度或动态大小的数组.