动态阵列推送功能 - 它是如何工作的?

时间:2013-04-27 02:26:15

标签: c++ arrays templates dynamic dynamic-arrays

我知道这是一大块代码,但我有一个问题,希望有人可以帮助...我试图构建一个模板化的可扩展数组类,并使用它来构建一个指向结构的指针数组。我的问题是在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;
}

1 个答案:

答案 0 :(得分: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每次推动都会调整大小)