#include "stdafx.h"#include <iostream>#include <ctype.h>#include <algorithm>Go to the source code of this file.
Namespaces | |
| namespace | std |
Compounds | |
| class | kvector |
| The kvector class. More... | |
| class | kvector.iterator |
| internal public iterator class More... | |
Functions | |
| void | vector_int () |
| run test code on integers | |
| void | vector_float () |
| run test code on floats | |
| int | main (int argc, char *argv[]) |
Variables | |
| const int | DEFSIZE = 5 |
| default vector size | |
| const int | INCR = 5 |
| how much to increase the array size by | |
|
||||||||||||
|
Definition at line 365 of file kvector.cpp. References vector_float(), and vector_int().
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 }
|
|
|
run test code on floats
Definition at line 295 of file kvector.cpp. References kvector< MYTYPE >::access(), kvector< MYTYPE >::add(), kvector< MYTYPE >::begin(), kvector< MYTYPE >::end(), kvector< MYTYPE >::get_length(), and kvector< MYTYPE >::show(). Referenced by main().
00296 {
00297 kvector<double> v;
00298
00299 try {
00300 // fill up the vector
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 since the [] operator returns a reference, we can treat it like a variable
00314 and use it on the left side of an asignment
00315 */
00316 cout << " changing element 0 to 99.4 using []" << endl;
00317 v[0] = 99.4;
00318 cout << v;
00319
00320 // try the access method on either side of the =
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 now test out the iterator class
00337 since the iterator class is inside the kvector class,
00338 we have to use the scope resolution operator (::) to use it.
00339 We will also test the iterator by using the copy algorithm
00340 from the STL.
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 } // vector_float
|
|
|
run test code on integers
Definition at line 225 of file kvector.cpp. References kvector< MYTYPE >::access(), kvector< MYTYPE >::add(), kvector< MYTYPE >::begin(), kvector< MYTYPE >::end(), kvector< MYTYPE >::get_length(), and kvector< MYTYPE >::show(). Referenced by main().
00226 {
00227 kvector<int> v;
00228
00229 try {
00230 // fill up the vector
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 /* since the [] operator returns a reference, we can treat it like a variable
00243 and use it on the left side of an asignment
00244 */
00245 cout << " changing element 0 to 99 using []" << endl;
00246 v[0] = 99;
00247 cout << v;
00248
00249 // try the access method on either side of the =
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 now test out the iterator class
00266 since the iterator class is inside the kvector class,
00267 we have to use the scope resolution operator (::) to use it.
00268 We will also test the iterator by using the copy algorithm
00269 from the STL.
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 } // vector_int
|
|
|
default vector size
Definition at line 17 of file kvector.cpp. Referenced by kvector< MYTYPE >::kvector(). |
|
|
how much to increase the array size by
Definition at line 19 of file kvector.cpp. Referenced by kvector< MYTYPE >::add(). |
1.3.3