调整指针数组c ++

时间:2012-11-12 20:34:24

标签: c++ arrays object pointers resize

让我说我有这个

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

3 个答案:

答案 0 :(得分:1)

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

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

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

现在你的数组大小是原始数据的两倍,并且包含原始数据。

答案 1 :(得分: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]')的原因。但是您可以将任意大小的指针指定给另一个指向数组的指针(相同类型)。因此,要扩展数组,首先要创建一个更大的数组,将较小数组的内容复制到它,清理较小的数组,将指向较大数组的指针分配给较小的数组。

答案 2 :(得分:-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];}
};

通过这项基本功能,您可以完成工作。