00001
00002
00003 #include "stdafx.h"
00004 #include <iostream>
00005 #include <ctype.h>
00006 #include <algorithm>
00007 using namespace std;
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 const int DEFSIZE=5;
00018
00019 const int INCR=5;
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 template <class MYTYPE>
00031 class kvector {
00032 public:
00033 kvector(void);
00034 kvector(int);
00035 ~kvector(void) { if(vptr != NULL) delete [] vptr;}
00036
00037 void add(MYTYPE x);
00038 void show(void);
00039 int get_length(void) { return length;}
00040
00041
00042
00043
00044
00045 MYTYPE & access(int index)
00046 {
00047 if((index >= 0) && (index < length))
00048 return vptr[index];
00049 else
00050 throw "Bad index";
00051 }
00052
00053
00054
00055
00056
00057
00058
00059 MYTYPE & operator[](int index)
00060 {
00061 if((index >= 0) && (index < length))
00062 return vptr[index];
00063 else
00064 throw "Bad index";
00065 }
00066
00067
00068
00069
00070
00071 friend ostream & operator<<(ostream & os, kvector<MYTYPE> & k)
00072 {
00073 for(int i=0; i < k.get_length(); i++)
00074 os << k[i] << " ";
00075 os << endl;
00076 return os;
00077 }
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 class iterator {
00088 public:
00089 iterator() {ptr = 0;}
00090 iterator(MYTYPE * m) {ptr = m;}
00091 iterator(iterator & i) {ptr = i.get_ptr();}
00092 MYTYPE * get_ptr() { return ptr;}
00093
00094
00095
00096
00097
00098
00099 void operator++(int) { ptr++; }
00100 void operator++() { ++ptr; }
00101
00102
00103
00104 void operator=(iterator p) { ptr = p.get_ptr(); }
00105
00106
00107
00108 MYTYPE & operator*(void) { return *ptr;}
00109
00110
00111 bool operator==(iterator i) { return (this->ptr == i.get_ptr());}
00112 bool operator!=(iterator k) { return (this->ptr != k.get_ptr());}
00113 private:
00114 MYTYPE * ptr;
00115 };
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125 iterator begin() {
00126 iterator *x = new iterator(vptr);
00127 return *x;
00128 }
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 iterator end() {
00139 iterator *x = new iterator( (MYTYPE *)(&vptr[length-1]) + 1);
00140 return *x;
00141 }
00142
00143 protected:
00144 MYTYPE * vptr;
00145 int count;
00146 int length;
00147 };
00148
00149
00150 template <class MYTYPE>
00151 kvector<MYTYPE>::kvector()
00152 {
00153 length = DEFSIZE;
00154 count = 0;
00155 if( (vptr = new MYTYPE[length]) == NULL) {
00156 cerr << "No memory for vector of length " << length << endl;
00157 }
00158 }
00159
00160
00161 template <class MYTYPE>
00162 kvector<MYTYPE>::kvector(int size)
00163 {
00164 length = size;
00165 count = 0;
00166 if( (vptr = new MYTYPE[length]) == NULL) {
00167 cerr << "No memory for vector of length " << length << endl;
00168 }
00169 }
00170
00171
00172 template <class MYTYPE>
00173 void
00174 kvector<MYTYPE>::show(void)
00175 {
00176 cout << "vector = (";
00177 for(int i = 0; i < count; i++) {
00178 cout << vptr[i];
00179 if(i+1 != count) cout << ",";
00180 }
00181 cout << ")" << endl;
00182 }
00183
00184
00185
00186
00187
00188 template <class MYTYPE>
00189 void
00190 kvector<MYTYPE>::add(MYTYPE x)
00191 {
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207 if( count == length) {
00208 cout << "making new array" << endl;
00209 MYTYPE * tptr = new MYTYPE[length + INCR];
00210 if(tptr == NULL) {
00211 cerr << "No memory for new array" << endl;
00212 throw ("no memory");
00213 }
00214
00215 for(int i=0; i < count; i++) tptr[i] = vptr[i];
00216
00217 vptr = tptr;
00218 length += INCR;
00219 }
00220 vptr[count++] = x;
00221 }
00222
00223
00224 void
00225 vector_int()
00226 {
00227 kvector<int> v;
00228
00229 try {
00230
00231 cout << "putting in initial data" << endl;
00232 for(int i=0; i < v.get_length(); i++) v.add(i);
00233 v.show();
00234 cout << "now adding 5 more" << endl;
00235 for(int j=0; j < 5; j++) v.add(j);
00236 v.show();
00237
00238 cout << "print using the overloaded [] operator" << endl;
00239 for(int k=0; k < v.get_length(); k++) cout << v[k] << " ";
00240 cout << endl;
00241
00242
00243
00244
00245 cout << " changing element 0 to 99 using []" << endl;
00246 v[0] = 99;
00247 cout << v;
00248
00249
00250 cout << "element 1 is " << v.access(1) << endl;
00251 cout << " changing element 1 to 42 using access()" << endl;
00252 v.access(1) = 42;
00253 cout << v;
00254 cout << "testing the array boundary exception" << endl;
00255 v[100] = 87;
00256 }
00257 catch(char * str ) {
00258 cerr << str << endl;
00259 }
00260 catch(...) {
00261 cerr << "Some unknown exception caught" << endl;
00262 }
00263
00264
00265
00266
00267
00268
00269
00270
00271 kvector<int>::iterator ptr;
00272 try {
00273 cout << "printing the vector using iterator" << endl;
00274 ptr = v.begin();
00275 while(ptr != v.end()) {
00276 cout << *ptr << " ";
00277 ptr++;
00278 }
00279 cout << endl;
00280 cout << "printing the vector using copy()" << endl;
00281 copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
00282 cout << endl;
00283 }
00284 catch(char * str ) {
00285 cerr << str << endl;
00286 }
00287 catch(...) {
00288 cerr << "Some unknown exception caught" << endl;
00289 }
00290
00291 }
00292
00293
00294 void
00295 vector_float()
00296 {
00297 kvector<double> v;
00298
00299 try {
00300
00301 cout << "putting in initial data" << endl;
00302 for(int i=0; i < v.get_length(); i++) v.add(i/3.0);
00303 v.show();
00304 cout << "now adding 5 more" << endl;
00305 for(int j=0; j < 5; j++) v.add(j/3.0);
00306 v.show();
00307
00308 cout << "print using the overloaded [] operator" << endl;
00309 for(int k=0; k < v.get_length(); k++) cout << v[k] << " ";
00310 cout << endl;
00311
00312
00313
00314
00315
00316 cout << " changing element 0 to 99.4 using []" << endl;
00317 v[0] = 99.4;
00318 cout << v;
00319
00320
00321 cout << "element 1 is " << v.access(1) << endl;
00322 cout << " changing element 1 to 42.4 using access()" << endl;
00323 v.access(1) = 42.4;
00324 cout << v;
00325 cout << "testing the array boundary exception" << endl;
00326 v[100] = 87.4;
00327 }
00328 catch(char * str ) {
00329 cerr << str << endl;
00330 }
00331 catch(...) {
00332 cerr << "Some unknown exception caught" << endl;
00333 }
00334
00335
00336
00337
00338
00339
00340
00341
00342 kvector<double>::iterator ptr;
00343 try {
00344 cout << "printing the vector using iterator" << endl;
00345 ptr = v.begin();
00346 while(ptr != v.end()) {
00347 cout << *ptr << " ";
00348 ptr++;
00349 }
00350 cout << endl;
00351 cout << "printing the vector using copy()" << endl;
00352 copy(v.begin(),v.end(),ostream_iterator<double>(cout," "));
00353 cout << endl;
00354 }
00355 catch(char * str ) {
00356 cerr << str << endl;
00357 }
00358 catch(...) {
00359 cerr << "Some unknown exception caught" << endl;
00360 }
00361
00362 }
00363
00364
00365 int main(int argc, char* argv[])
00366 {
00367 char ch;
00368 cout << "Enter i for integer test or f for float test" << endl;
00369 cin >> ch;
00370 cin.ignore();
00371 if(tolower(ch) == 'i')
00372 vector_int();
00373 else
00374 vector_float();
00375 return 0;
00376 }