c++ - 更改数组类以保存动态数组

标签 c++ arrays dynamic-arrays

我读过的所有内容都说这应该很简单,您只需添加这三行

typedef double* DoublePtr;
  DoublePtr p;
  p = new double [10]

但是我应该在哪里添加这段代码呢?我尝试过的一切都破坏了我的程序,我错过了什么?我尝试了一个 set 函数来设置最大尺寸的值,但它也没有用 有人知道怎么做吗?

#include<iostream>
using namespace std;
const int MAX_SIZE = 50;

class ListDynamic
{

    public:

        ListDynamic();
        bool full();
        int getSize();
        void addValue(double value);
        double getValue(int index);
        double getLast();
        void deleteLast();
        friend ostream& operator <<(ostream& out, const ListDynamic& thisList);  

    private:
        double listValues[MAX_SIZE];
        int size;
};
int main()
{
    double value;
    ListDynamic l;
    cout << "size of List " << l.getSize() << endl;



    cout << "New size of List " << l.getSize() << endl;
    cout << "First Value: " << l.getValue(0) << endl;
    cout << "Last Value: " << l.getLast() << endl;
    cout << "deleting last value from list" << endl;
    l.deleteLast();
    cout << "new list size "  << l.getSize() << endl;
    cout << "the list now contains: " << endl << l << endl;
    system("pause");
    return 0;
}

ListDynamic::ListDynamic()
{
    size = 0;
}

bool ListDynamic::full()
{
    return (size == MAX_SIZE);
}

int ListDynamic::getSize()
{
    return size;
}

void ListDynamic::addValue(double value)
{
    if (size < MAX_SIZE)
    {
        listValues[size] = value;
        size++;
    }
    else
        cout << "\n\n*** Error in ListDynamic Class: Attempting to add value past max limit.";
}

double ListDynamic::getValue(int index)
{
    if (index < size)
        return listValues[index];
    else
        cout << "\n\n*** Error in ListDynamic Class: Attempting to retrieve value past current size.";
}
double ListDynamic::getLast()
{
    if (size > 0)
        return getValue(size - 1);
    else
        cout << "\n\n*** Error in ListDynamic Class: Call to getLast in Empty List.";
}

void ListDynamic::deleteLast()
{
    if (size > 0)
        size--;
    else
        cout << "\n\n*** Error in ListDynamic Class: Call to deleteLast in Empty List.";
}
ostream& operator <<(ostream& out, const ListDynamic& thisList)
{
    for (int i = 0; i < thisList.size; i++)
        out << thisList.listValues[i] << endl;
    return out;
}  

最佳答案

您需要将 listValues 更改为 double*

double* listValues;

当你添加一个大于大小的值时,你需要重新分配数组你的数组并将前一个数组的元素复制到新数组中。例如:

void ListDynamic::addValue(double value)
{
    if (full())
    {
        double* temp = new double[size];
        std::copy(listValues, listValues + size, temp);

        delete[] listValues;
        listValues = new double[size + 1];

        std::copy(temp, temp + size, listValues);
        listValues[size] = value;
        delete[] temp;
    } else
    {
        listValues[size++] = value;
    }
}

关于c++ - 更改数组类以保存动态数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20035815/

相关文章:

arrays - 在 "out of order"数组中查找数字的最佳方法?

c++ - 动态链接库的问题

c++ - 如何在实时绘图中使用 QCustomPlot

c++ - 在破坏的对象上 move 构造函数?

c - 如何为二维数组动态分配内存

c++ - 在 C++ 中将类构造函数分配给带有参数的新构造函数

c++ - 如何使用(Boost Multidimensional Array Library)构造动态二维数组?

c++ - 如何为模板的内部类型专门化一个类?

javascript - 在 js 或 jquery 中轻松创建数组,就像在 python 中一样

arrays - 在这种情况下使用哪种搜索算法更好?