将元素推送到动态分配的指针数组int C ++

时间:2015-06-28 02:39:43

标签: c++ arrays pointers object

[编辑1]我将序言说,对于这个项目,我需要“创建一个容器类”,我可以从列表中推送,弹出和检索元素。我应该使用指针,并且必须编写推送,弹出等功能。[/ EDIT 1]

我很难将元素推送到动态分配的指针数组。我能够最初创建一个数组就好了。您可以从我的评论中看到我的思维过程,我认为我应该能够添加到数组中:

1) create new array with room for the one new element;
2) add the new element to index 0;
3) copy the old array into the rest of the new array;
4) delete old array;
5) set the new array as the value of the pointer

我有以下三个文件:

IntegerList.h:

/** 
*IntegerList.h
*/

#ifndef IntegerList_H
#define IntegerList_H

class IntegerList
{
private:
    int * pArray;
    int length;

public:
    IntegerList(); // default constructor

    void createArray(int howlong);
    int getValue(int index);
    void deleteArray();

    void pushFront(int element);
};

#endif

IntegerList.cpp:

/**
* IntegerList.cpp
*/

#include "IntegerList.h"
#include <iostream>

using namespace std;

// IntegerList constructor:
IntegerList::IntegerList()
{
    pArray = 0;
    length = 0;
}


// creates an array of length howlong (determined by main.cpp); sets the values
// to equal ten times the index number. For example, if an array of size 4 is
// to be created, then an array with the following values will be created by
// this method: 0, 10, 20, 30. Sets length equal to howlong.
void IntegerList::createArray(int howlong)
{
    length = howlong;
    pArray = new int[length];
    for (int i = 0; i < length; i ++)
        pArray[i] = (i*10);
}


int IntegerList::getValue(int index)
{
    return pArray[index];
}


void IntegerList::deleteArray()
{
    delete[] pArray;
}


// places element at front of array
void IntegerList::pushFront(int element)
{
    // create new array with room for the one new element
    int newArray[length+1]; // nope

    // start by adding the new element
    newArray[0] = element;

    // copy the old array, put it into the new array starting at index 1 (since
    // index 0 is the new element)
    for(int i = 0; i < length; i ++)
    {
        newArray[i+1] = pArray[i];
    }

    // delete old array
    deleteArray();

    // set pArray equal to the new array;
    pArray = newArray;

    // update the value of length
    length += 1;
}

我的主文件main.cpp:

#include "IntegerList.h"
#include <iostream>

using namespace std;

int main()
{
    // create object
    IntegerList myArray;

    // create array of length 5
    myArray.createArray(5);

    // print array
    for (int i = 0; i < 5; i ++)
        cout << "Element " << i << ". " << myArray.getValue(i) << endl;

    // everything works ok so far

    // push the number 99 to front
    myArray.pushFront(99);

    // print array
    for (int i = 0; i < 6; i ++)
        cout << "Element " << i << ". " << myArray.getValue(i) << endl;

    myArray.deleteArray();
}

第一个printArray()表明一切都按计划进行。然而,在我尝试将99推到前面之后,事情就搞砸了:(

以下是我得到的输出:

Element 0. 0
Element 1. 10
Element 2. 20
Element 3. 30
Element 4. 40

Element 0. 99
Element 1. 0
Element 2. 2130567168
Element 3. 4486648
Element 4. 2686508
Element 5. 4201772

请注意,在第二个打印输出中,前两个元素似乎具有我想要的值。

有什么建议吗?

2 个答案:

答案 0 :(得分:0)

我不确定你是否可以强行制作可变长度的静态数组,但这违反了标准。

int newArray[length+1];

不起作用。做到这一点

int * newArray = new int[length+1];

代替。

正如评论中的其他人所说,在每次插入后重新分配数组是非常低效和繁琐的工作。考虑使用其中一些:

  • std::vector
  • std::stack(当您在容器的开头插入时)
  • std::list
  • 您自己的链接列表

答案 1 :(得分:0)

我重写了推送代码并使其正常工作。修改后的代码如下。请注意,我必须将tempArray分配回&#34;原文&#34;数组(pArray),而不是tempArray的地址。

void IntegerList::push(int value){
    // when this method is called to push a value onto an empty array, that is,
    // when length = 0, it first creates an array of length 1
    if(length == 0)
        pArray = new int[1];

    // create temp array
    int * tempArray = new int[length+1];

    // add new value to index 0
    tempArray[0] = value;

    // add old elements
    for(int i = 0; i < length; i++)
        tempArray[i+1] = pArray[i];

    delete[] pArray;

    // initialize pArray with proper size
    pArray = new int[length+1];

    // copy elements of tempArray into pArray
    for(int i = 0; i < length+1; i++)
        pArray[i] = tempArray[i];

    delete[] tempArray;

    length += 1;
} // end push