我创建了一个方法,该方法应该删除数组的第一个元素,但是,当我运行代码时,调试器将翻转出来,我不确定为什么。

这是我的removeFirst()方法:

Loan & ListOfLoans ::  removeFirst(){
    index = 0;
    //determine if the container needs to be shrunk
    if((numberOfElements < capacity/4) && (capacity >= 4)){ // shrink container when     array is 1/4 full
        cout<<"shrinking array! \n";
        Loan ** temp = elements;
        elements = new Loan * [numberOfElements/2];
        //copy temp array to elements
        for(int i = 0; i<numberOfElements; i++){
            temp[i] = elements[i];
            numberOfElements = numberOfElements/2;
            delete [] temp;
        }
    }
    numberOfElements--;
    return **elements;
}


和我的头文件很好地衡量:

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

using namespace std;

class ListOfLoans {

public:
ListOfLoans(int initial_size=4);

~ListOfLoans(void);


 void add(Loan & aLoan);
 Loan & first() ;
 Loan & removeFirst();
   // answer the first element from the list and remove it from the list
    // if the resulting list is more than three quarters empty release some memory
 Loan & removeLast();
   // answer the last element from the list and remove it from the list
    // if the resulting list is more than three quarters empty release some memory
 Loan & next();
 int size();

private:
Loan ** elements; //actuall stuff in the array m_pnData;
int numberOfElements; //number of elements in the list  stize of the array? m_nLength
int capacity; //size of the available array memory
int index; //used to help with the iteration
};

最佳答案

此代码有很多问题。这是带注释的副本。

// Return by reference cannot be used if you are removing the element.
// This looks like a Java-ism.
Loan & ListOfLoans ::  removeFirst(){
    // unused variable
    index = 0;

    if((numberOfElements < capacity/4) && (capacity >= 4)){
        cout<<"shrinking array! \n";
        Loan ** temp = elements;
        // you are allocating an array of size numberOfElements/2...
        elements = new Loan * [numberOfElements/2];

        // then accessing elements which are way past its end.
        for(int i = 0; i<numberOfElements; i++){
            temp[i] = elements[i];
            // you are halving the array size in every iteration
            numberOfElements = numberOfElements/2;
            // you are deleting temp in every iteration,
            // causing double-frees
            delete [] temp;
        }
    }
    // if the array does not need to be shrunk,
    // you are actually removing the last element.
    // the removed element is not freed and is leaking.
    numberOfElements--;
    // you are returning the first element,
    // which is not removed.
    return **elements;
}


我强烈建议用STL容器(例如ListOfLoans)替换std::deque。如果您无法执行此操作,则此为最低固定版本。

Loan ListOfLoans::removeFirst() {
    Loan to_return;
    if (numberOfElements == 0) {
        return to_return; // return default value, there is nothing to remove
    }
    if ((numberOfElements < capacity/4) && (capacity >= 4)) {
        Loan **old = elements;
        capacity = capacity / 2;
        elements = new Loan*[capacity];
        for (int i=0; i < numberOfElements - 1; ++i) {
            elements[i] = old[i+1];
        }
        to_return = *old[0];
        delete old[0];
        delete[] old;
    } else {
        to_return = *elements[0];
        delete elements[0];
        for (int i=0; i < numberOfElements - 1; ++i) {
            elements[i] = elements[i+1];
        }
    }
    --numberOfElements;
    return to_return;
}

关于c++ - 数组,removeFirst()不起作用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19214314/

10-09 23:41