gpt4 book ai didi

c++ - 动态数组推送功能 - 它是如何工作的?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:27:55 25 4
gpt4 key购买 nike

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

最佳答案

所以 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 在每次推送时调整大小)

关于c++ - 动态数组推送功能 - 它是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16247681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com