i know this is a large chunk of code, but im having an issue that hopefully someone can help with... im trying to build an templated expandable array class and use it to build an array of pointers to a struct. my issue is in main(), im not sure how to implement the pushElement function to include more pointers to structs in the if statement.
any help would be awesome, im pretty new to the forum and thank you in advance for not being too harsh...
#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;
}
So DArray<T>
is just like std::vector<T>
, except a lot less efficient.
To use DArray<T>
in a loop:
DArray<T> a;
while (have_more_data)
{
T t;
t.foo = ...;
t.bar = ...;
t.baz = ...;
a.pushElement(t);
}
std::vector
is superior as (1) it uses move semantics, and (2) only resizes the backing store on powers of two so it has amortized constant O(1) push_back
time. (whereas DArray resizes on every push)