0

假设我有这个

#define T 10

StackOverflow *_stObject[H];

如何将此数组“调整”为 20?我不能使用向量。我必须将这 11 个位置复制到另一个具有 20 个位置的指针数组,但是这样我就不能在我的其他函数中使用这个对象。

这个对象存储数据,当它满了我需要找到一种方法来继续添加数据。

我怎样才能做到这一点?

好的,这里有更多信息,因为它不起作用。我做了方法 extendArray() 但是当我做 r = temp 时它有一个错误

这是一个计算器,Reg 类存储了我在计算器中进行的操作的信息。对象“r”存储了 10 个操作,如果我进行超过 10 个操作,我必须扩展这个指针数组。

我收到的错误消息在 r = temp 中,它说:'Reg* [20]' 到 'Reg* [10]' 的分配中的类型不兼容|

#define T 10

   class Calculator
    {
        public:
            Calculator();
            Calculator(NumComp&,NumComp&);
            ~Calculator();

            void printVisor();
            void setCalculator(const NumComp&,const NumComp&);
            void menu();
            void help();
            void clean();
            void run();
            void extendArray();

        private:
            NumComp n1, n2;
            Reg *r[T];
            int _tMax;
    };

        void Calculator::extendArray()
    {
        Reg *temp[T*2];

        for(int i = 0; i < 5; i++){
            temp[i] = new Reg;
            temp[i] = r[i];
          delete r[i];
         }

        r = temp;
        _tMax *= 2;
    }
4

3 回答 3

1

您必须创建一个新数组并进行深层复制。

 StackOverflow * array = new StackOverFlow[(H * 2)];

 for (int i = 0; i < H; i++)
       arrary[i] = _stObject[i];

现在您有一个数组,其大小是原始数组的两倍,其中包含原始数据的所有数据。

于 2012-11-12T20:38:32.440 回答
0

如果您的作业/作业表明您不能使用任何 STL 容器,您可以执行以下操作

#define T 10

class NumComp {
// ---
};

class Reg {
// ---
};


class Calculator
{
     public:
         Calculator();
         Calculator(NumComp&,NumComp&);
         ~Calculator();

         void printVisor();
         void setCalculator(const NumComp&,const NumComp&);
         void menu();
         void help();
         void clean();
         void run();
         void extendArray();

     private:
         NumComp n1, n2;
         Reg** r;
         int _tMax;

         void createArray();
         void cleanupArray(); 
};


Calculator::Calculator() {
    createArray();
}

Calculator::Calculator(NumComp&,NumComp&) {
    createArray();
}

Calculator::~Calculator() {
    cleanupArray();
}

void Calculator::createArray() {

    // create a pointer array
    r = new Reg*[T];

    // initialize to nulls
    for (int i = 0; i < T; i++) {
        r[i] = 0;
    }

    _tMax = T;
}

void Calculator::cleanupArray() {

    // make sure the contents of r[] are deleted by whoever allocates them
    delete [] r;
    r = 0;
}


void Calculator::extendArray()
{
    Reg** temp = new Reg*[_tMax*2];

    // copy contents of r to temp
    for(int i = 0; i < _tMax; i++) {
        temp[i] = r[i];
    }

    // initialize rest of temp
    for (int i = _tMax - 1; i < _tMax*2; i++) {
        temp[i] = 0;
    }

    // delete what was originally in r
    delete [] r;
    r = temp;

    _tMax *= 2;
}

扩展数组的唯一方法是将其内容复制到更大的数组中。您不能简单地将较大的数组分配给较小的数组(这就是您收到错误的原因incompatible types in assignment of 'Reg* [20]' to 'Reg* [10]')。但是您可以将任意大小的指向数组的指针分配给另一个指向数组的指针(相同类型)。所以要扩展一个数组,首先创建一个更大的数组,将较小数组的内容复制到它,清理较小的数组,将指向较大数组的指针分配给较小的数组。

于 2013-03-14T22:53:47.333 回答
-1

如果您不能使用std::vector(这将是最合理的使用方式),那么您可以编写自己的具有简化功能的“向量”:

class MyVector
{
    int size;
    int capacity;
    StackOverFlow ** data;

    public:

    MyVector() : size(0), capacity(10), data(new StackOverFlow * [10]) {}
    ~MyVector() {delete data;}

    void duplicateCapacity()
    {
        capacity *= 2;
        StackOverFlow ** tmp = new StackOverFlow * [capacity];
        for(int i=0; i<size; i++)
             tmp[i] = data[i];
        delete data; //<-- here was an error...
        data = tmp;
    }

    void add(StackOverFlow * item)
    {
        if(size == capacity)
            duplicateCapacity();
        data[size++] = item; 
    }

    StackOverFlow * get(int index) {return data[index];}
};

有了这个基本功能,您就可以完成工作。

于 2012-11-12T22:31:54.300 回答