00001
00006 #include <cassert>
00007
00008
00009
00010
00011 template<class T>
00012 Vector<T>::Vector<T>(int n= 0)
00013 {
00014 assert(n>=0);
00015 if (n>0)
00016 datos= new T[n];
00017 nelementos= n;
00018 }
00019
00020
00021 template<class T>
00022 Vector<T>::Vector<T>(const Vector<T>& original)
00023 {
00024 nelementos= original.nelementos;
00025 if (nelementos>0) {
00026 datos= new T[nelementos];
00027 for (int i=0; i<nelementos;++i)
00028 datos[i]= original.datos[i];
00029 }
00030 else datos=0;
00031 }
00032
00033
00034 template<class T>
00035 Vector<T>::~Vector<T>()
00036 {
00037 if (nelementos>0) delete[] datos;
00038 }
00039
00040
00041 template<class T>
00042 int Vector<T>::size() const
00043 {
00044 return nelementos;
00045 }
00046
00047
00048 template<class T>
00049 T& Vector<T>::operator[] (int i)
00050 {
00051 assert (0<=i && i<nelementos);
00052 return datos[i];
00053 }
00054
00055
00056 template<class T>
00057 const T& Vector<T>::operator[] (int i) const
00058 {
00059 assert (0<=i && i<nelementos);
00060 return datos[i];
00061 }
00062
00063
00064 template<class T>
00065 void Vector<T>::resize(int n)
00066 {
00067 assert (n>=0);
00068 if (n!=nelementos) {
00069 if (n!=0) {
00070 T * nuevos_datos;
00071 nuevos_datos= new T[n];
00072 if (nelementos>0) {
00073 int minimo;
00074 minimo= nelementos<n?nelementos:n;
00075 for (int i= 0; i<minimo;++i)
00076 nuevos_datos[i]= datos[i];
00077 delete[] datos;
00078 }
00079 nelementos= n;
00080 datos= nuevos_datos;
00081 }
00082 else {
00083 if (nelementos>0)
00084 delete[] datos;
00085 datos= 0;
00086 nelementos= 0;
00087 }
00088 }
00089 }
00090
00091
00092 template<class T>
00093 Vector<T>& Vector<T>::operator= (const Vector<T>& original)
00094 {
00095 if (this!= &original) {
00096 if (nelementos>0) delete[] datos;
00097 nelementos= original.nelementos;
00098 datos= new T[nelementos];
00099 for (int i=0; i<nelementos;++i)
00100 datos[i]= original.datos[i];
00101 }
00102 return *this;
00103 }