简体   繁体   中英

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

I'd like to implement a class with an array. If the size of the array is being breached, I would call a resize() function. However I can't seem to code a dynamic array as a data member. Can someone guide me on how to work around this?

This is what I got so far:

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;
};

Use std::vector . C++ doesn't support variable-length or dynamically-sized arrays, in classes or otherwise.

Use a std::vector and let it manage everything for you:

#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;
};

If you really want to manage the array memory yourself, you are doing it all wrong. Try this instead:

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;
};

I don't have reputation to leave you a comment yet, but have you tried C++'s std::vector ? It does exactly what you're looking for:

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

It sounds like you should be using malloc and realloc . This should work the same as Remy's answer using new and delete.

Declare your buffer as an int* and keep track of the size with a variable that you increment. If your size exceeds the amount of space in your dynamic array, reallocate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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