ok

#ifndef finalproject_Header_h
#define finalproject_Header_h

#include <iostream>
#include <string>
#include <cassert>
#include <exception>

using namespace std;

template <class T>
class myvector
{
private:
int capacity_of_vector;
int size_of_vector;
T* array;

public:
//return the current length of the vector
int size()
{
return size_of_vector;
}
// return the current capacity of the vector
int capacity()
{
return capacity_of_vector;

}
//return whether or not the vector is empty
bool empty();
// resize the vector (change length) to the specified size, changing capacity as needed
void resize(int);
//change the vector capacity to the specified size, or give an error if capacity would get smaller than length
void reserve(int);
//assign the second argument as the value of the vector at the position of the first argument
void assign(int, T);
//increase length by 1 and add the argument as the value at the end
void push_back(T);
//decrease length by 1, returning the value removed from the vector
T pop_back();
//increase length by 1, put the value specified in the second argument at index specified by first argument and shift all values to the right in the vector down by one index
void insert(int, T);
//decrease length by 1, remove the item at the specified index and shift all other items left to eliminate the "hole"
void erase(int);
//same as erase(int), but removes all elements between the indexes specified by the first and second argument
void erase(int, int);
//remove all elements from the list
void clear();
//constructors
myvector();
myvector(int);
myvector(const myvector &);
myvector(const T*, int);
~myvector();
T &operator[](int);
T &at(int);
};

//bool empty()
template <class T>
bool myvector<T>::empty()
{
if (this->size_of_vector == 0)
return true;
else
return false;
}

//void resize(int)
template <class T>
void myvector<T>::resize(int size)
{
if (size >= 0)
{
if (size>capacity_of_vector)
{
capacity_of_vector = size;
T *array2 = array;
array = new T[capacity_of_vector];
for (int i = 0; i<size_of_vector; i++)
{
array[i] = array2[i];
}
delete[] array2;
}
size_of_vector = size;
}
else
{
throw exception("Error:Size must equals or bigger than 0!");
}
}

//void reserve(int)
template <class T>
void myvector<T>::reserve(int length)
{
if (size_of_vector <= length)
{
capacity_of_vector = length;
T *array2 = array;
array = new T[capacity_of_vector];
for (int i = 0; i<size_of_vector; i++)
{
array[i] = array2[i];
}
delete[] array2;
}
else{
throw exception("Error:overflow because capacity would be smaller than length!");
}
}

//myvector()
template <class T>
myvector<T>::myvector()
{
array = new T[0];
capacity_of_vector = size_of_vector = 0;
}
//~myvector()
template <class T>
myvector<T>::~myvector()
{
delete[] array;
}
//myvector(int):initial length of the vector
template <class T>
myvector<T>::myvector(int lengs)
{
array = new T[lengs];
size_of_vector = lengs;
}
//myvector(const myvector &):copy the contents of the vector
template <class T>
myvector<T>::myvector(const myvector & temp)
{
if (this == &temp)
return;
array = new T[temp.capacity_of_vector];
capacity_of_vector = temp.capacity_of_vector;
size_of_vector = temp.size_of_vector;
for (int i = 0; i<temp.size_of_vector; i++)
array[i] = temp.array[i];

}
// myvector(const T*,int):copy contents of array into the vector
template <class T>
myvector<T>::myvector(const T *P, int x)
{
size_of_vector = x;
capacity_of_vector = size_of_vector;
array = new T[size_of_vector];
for (int i = 0; i<size_of_vector; i++)
{
array[i] = P[i];
}

}

//[]operator:return a member of the vector at the index specified
template <class T>
T& myvector<T>::operator[](int index)
{
if (index >= 0 && index<this->size_of_vector)
{
return array[index];
}
else if (index < 0)
{
throw exception("Error:Index less than 0!");
}
else
{
throw exception("Error:Index exceed length!");
}
}
//function "at":perform the exact same functionality with []operator
template <class T>
T& myvector<T>::at(int index)
{
if (index >= 0 && index<this->size_of_vector)
{
return array[index];
}
else if (index < 0)
{
throw exception("Error:Index less than 0!");
}
else
{
throw exception("Error:Index exceed length!");
}
}
//void assign(int,T)
template <class T>
void myvector<T>::assign(int index, T content)
{
if (index<=this->size_of_vector&&index > 0)
{
this->array[index - 1] = content;
}
else
{
throw exception("Error: index must between 0 and size of vector!");
}
}
//void push_back(T)
template <class T>
void myvector<T>::push_back(T index)
{
if (size_of_vector + 1 <= capacity_of_vector)
{
size_of_vector = size_of_vector + 1;

}
else
{
size_of_vector++;
capacity_of_vector = size_of_vector;
T *array2 = array;
array = new T[capacity_of_vector];
for (int i = 0; i<size_of_vector; i++)
{
array[i] = array2[i];
}
delete[] array2;
//throw exception("the capacity is not enough for push back!!");
}
array[size_of_vector - 1] = index;
}
//T back()
template <class T>
T myvector<T>::pop_back()
{
T x;
x = array[size_of_vector - 1];
resize(size_of_vector - 1);
return x;
}

//void insert(int, T)
template <class T>
void myvector<T>::insert(int index, T content)
{
if (index <= 0 || index > size_of_vector)
throw exception("Error: index must between 0 and size of vector!");
else
{
/*if (size_of_vector + 1 <= capacity_of_vector)
{
size_of_vector = size_of_vector + 1;
}
else
{
throw exception("Error: insert exceed the size of vector!");
}*/
for (int i = size_of_vector - 1; i >= index; i--)
{
array[i] = array[i - 1];
}
array[index - 1] = content;
}
}


//void erase(int)
template <class T>
void myvector<T>::erase(int index)
{
if (index <= 0 || index > size_of_vector)
throw exception("Error: index must between 0 and size of vector!");
else
{
for (int i = index - 1; i<size_of_vector; i++)
{
array[i] = array[i + 1];
}
resize(size_of_vector - 1);
}
}
//void erase(int, int)
template <class T>
void myvector<T>::erase(int index1, int index2)
{
if (index1 <= 0 || index2 <= 0 || index1>index2 || index2>size_of_vector)
{
throw exception("Error! Check: 1.The right index should bigger than the left one 2.index should between 0 and size of vector");
}
else
{
for (int i = index1 - 1; i<size_of_vector - index2 + 1; i++)
{
array[i] = array[i + (index2 - index1 + 1)];
}
resize(size_of_vector - (index2 - index1 + 1));
}

}
//void clear()
template <class T>
void myvector<T>::clear()
{
capacity_of_vector = 0;
size_of_vector = 0;
}

#endif

posted @ 2013-12-05 10:13  Guybrush Threepwood  阅读(149)  评论(0)    收藏  举报