1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
#include "intarray.h"
std::ostream & operator<< (std::ostream & o, intarray & A)
{
for (int i = 0; i <= A.len - 2; i++)
o << A.data[i] << ", ";
o << A.data[A.len - 1];
return o;
}
intarray::intarray (const intarray &A)
{
this->len = A.len;
this->data = new int [A.len];
for (int i = 0; i < A.len; i++)
this->data[i] = A.data[i];
}
int intarray::length ()
{
return this->len;
}
intarray::intarray ()
{
this->len = INTARRAY_DEFAULT_ALLOC;
this->data = new int[INTARRAY_DEFAULT_ALLOC];
}
intarray::intarray (int len)
{
this->len = len;
this->data = new int[len];
}
intarray::~intarray ()
{
delete[] this->data;
}
int intarray::get (int index)
{
return this->data[index];
}
void intarray::set (int index, int n)
{
this->data[index] = n;
}
extintarray::extintarray () : intarray ()
{
this->alloc = INTARRAY_DEFAULT_ALLOC;
}
extintarray::extintarray (int alloc)
{
this->len = 0;
this->alloc = alloc;
this->data = new int[alloc];
}
extintarray::extintarray (int len, int alloc)
{
this->len = len;
this->alloc = alloc;
this->data = new int[alloc];
}
extintarray::~extintarray ()
{
delete[] this->data;
}
void extintarray::set (int index, int n)
{
if (index >= this->alloc)
this->alloc_more (index * 2);
if (index >= this->len)
this->len = index + 1;
this->data[index] = n;
}
void extintarray::alloc_more (int newsize)
{
int* new_data = new int[newsize];
for (int i = 0; i < this->len; i++)
new_data[i] = this->data[i];
delete[] this->data;
this->data = new_data;
this->alloc = newsize;
} |