Как вставить обратную ссылку в массив Dynami c, заполненный по прибытии? - PullRequest
0 голосов
/ 09 мая 2020

Я реализую динамический c массив; основная проблема заключается в том, что когда я вызываю функцию insertBack(), элемент управления никогда не попадет внутрь, потому что условие выполняется, т. е. поскольку емкость равна 0, а n (размер) также равен 0; он никогда не может go внутри кодового блока. Как мне go решить эту проблему? Я пробовал сделать перегруженный конструктор и передать определенную емкость - не вышло.

#include <iostream>

template<typename T>
class container
{
    template <typename T2>
    friend std::ostream& operator<<(std::ostream& out, const container<T2> &cobj);
public:
    container();
    ~container();
    bool isEmpty() const;
    bool isFull();
    int size() const;
    int capacity() const;
    bool insertBack(const T& val);
private:
    void allocate(T* &temp);
    T *arr;
    int Capacity;
    int n;  
};

template<typename T2>
std::ostream& operator<<(std::ostream& out, const container<T2> &cobj)
{
    std::cout << "Currently it contains " << cobj.size() << " value(s)" << std::endl
        << "Container storage capacity = " << cobj.capacity() << std::endl
        << "The contents of the container:" << std::endl;

    if (cobj.isEmpty())
    {
        std::cout << "*** Container is currently empty!" << std::endl;
    }
    else
    {
        for (int i=0; i<cobj.size(); ++i)
        {
            std::cout << cobj.arr[i] << " ";
        }
    }

    return out;
}

template<typename T>
container<T>::container()
{
    arr = new T[Capacity];
    n = 0;
}

template<typename T>
container<T>::~container()
{
    delete []arr;
    arr = nullptr;
    std::cout << "Destructor called! (this line is normally not displayed)" << std::endl;
}

template<typename T>
bool container<T>::isEmpty() const
{
    return n==0;
}

template<typename T>
bool container<T>::isFull()
{
    return n==Capacity;
}

template<typename T>
int container<T>::capacity() const
{
    return Capacity;
}

template<typename T>
int container<T>::size() const
{
    return n;
}

template<typename T>
bool container<T>::insertBack(const T& val)
{
    if (!isFull())
    {
        T* old_array = arr; 
        for (int i=0; i<n; ++i)
        {
            arr[i] = old_array[i];
        }

        arr[n] = val;
        n++;
        delete[] old_array;

        return true;
    }
    else
    {
        allocate(arr);
        return false;
    }
}

template<typename T>
void container<T>::allocate(T* &temp)
{
    if (Capacity==0)
    {
        temp = new T;
    }
    else
    {
        temp = new T[Capacity <<= 1];
    }
}

int main()
{
    container<int> a1;
    std::cout << a1 << std::endl; 
    std::cout << "Currently, the container object contains 0 element(s) or 0 value(s)" << std::endl;

    std::cout << "\nWe now insert 5 values at the back of the array, one at a time:" << std::endl;

    const int num = 5;
    for (int i=1, c=10; i<=num; ++i, c+=10)
    {
        a1.insertBack(c);
        std::cout << a1 << std::endl << std::endl;
    }
}
...