2

我知道这是一大段代码,但我遇到了一个希望有人能帮助解决的问题......我正在尝试构建一个模板化的可扩展数组类并使用它来构建一个指向结构的指针数组。我的问题在 main() 中,我不确定如何实现 pushElement 函数以在 if 语句中包含更多指向结构的指针。

任何帮助都会很棒,我对论坛很陌生,提前感谢您不要太苛刻......

#include<fstream>
#include<iostream>
#include<cstdlib>
#include<string>
#include "Loan.h"
#include "SimpleLoan.h"
#include "AmatorizedLoan.h"
#include "pa4functions.h"


#ifndef DARRAY_H
#define DARRAY_H

template <class T>
class DArray{


    public:
        DArray(); 
        DArray(int length);
        ~DArray();

        T& operator[](int index); // the indexing operation

        void setElement(int i, const T& newVal);
        void eraseElement(int i);
        void addElement(int index, const T& newVal);
        void pushElement(const T& newVal);
        void display() const;

    private:
        T* array;
        int size;
        int nextIndex;
};
#endif

template <class T>
DArray<T>::DArray(int length){
    size = length;

    array = new T[size];

    nextIndex = 0;
}

template <class T>
DArray<T>::~DArray(){ 
    delete[] array; array = NULL;}

template<class T>
void DArray<T>::setElement(int i, const T& newVal){
    if (i < size && i >= 0)
        {array[i] = newVal;}
}

template<class T>
void DArray<T>::eraseElement(int index){
    size -= 1;

    T* newDArray = new T[size];

    for (int i = 0; i< size +1; i++)
    {
        if (i < index)
            {newDArray[i] = array[i];}

        else if (i > index)
            {newDArray[i - 1] = array[i];}
    }

    delete [] array;
    array = newDArray;
}   

template <class T>
void DArray<T>::addElement(int index, const T& newVal){
    if (index < size+1 && index >= 0)
        {
            size +=1;

            T* newDArray = new T[size];

            for (int i=0;  i < size; i++)
            {
                if (i < index)
                    {newDArray[i] = array[i];}

                else if (i == index)
                    {newDArray[i] = newVal;}

                else
                    {newDArray[i] = array[i-1];}
            }

        delete [] array;
        array = newDArray;

    }
}

template<class T>
void DArray<T>::pushElement(const T& newVal){
    size += 1;
    T* newDArray = new T[size];

    for (int i = 0; i < size; i++)
        {newDArray[i] = array[i];}

    delete [] array;
    newDArray[size -1] = newVal;

    array = newDArray;
}

int main(int argc, char *argv[])
{
    using std::cout;
    using std::endl;
    using std::string;
    using std::cerr;

    pa4functions::displayGreeting();

    if(argc < 2)
        {
    cerr << "Error, Please enter the name of the file you want to read from\n";
    cerr << "followed by the file you wantto write to in the command line.\n";
        }

    //creates a file in stream  
    ifstream infile;

    //opens the file

    infile.open(argv[1]);

//create principal array
        int size = 3;
        int counter = 0;
        int theLength;
        DArray <ptrLoan> array(size);
        string token, line;
        double thePrincipal, theRate;

        while (getline(infile, line))
        {
            istringstream tokenizer(line);

            //itsPrincipal, itsRate, itsLength;
            getline(tokenizer, token, ' ');
            istringstream double_iss1(token);
            double_iss1 >> array[counter].itsPrincipal;

            getline(tokenizer, token, ' ');
            istringstream double_iss2(token);
            double_iss2 >> array[counter].itsRate;

            getline(tokenizer, token, ' ');
            istringstream int_iss(token);           
            int_iss >> array[counter].itsLength;
            counter++;
               if (counter <= size){

               }


        }
        infile.close();

    return EXIT_SUCCESS;
}
4

1 回答 1

1

DArray<T>就像 一样,std::vector<T>除了效率低得多。

DArray<T>在循环中使用:

DArray<T> a;
while (have_more_data)
{
    T t;
    t.foo = ...;
    t.bar = ...;
    t.baz = ...;
    a.pushElement(t);
}

std::vector优于 (1) 它使用移动语义,并且 (2) 仅根据 2 的幂调整后备存储的大小,因此它具有摊销常数 O(1)push_back时间。(而 DArray 在每次推送时都会调整大小)

于 2013-04-27T02:39:08.013 回答