Public Member Functions | |
kvector (void) | |
default constructor | |
kvector (int) | |
set initial vector size to size | |
~kvector (void) | |
void | add (MYTYPE x) |
void | show (void) |
print the current data elements in the vector | |
int | get_length (void) |
MYTYPE & | access (int index) |
access | |
MYTYPE & | operator[] (int index) |
overloaded array access operator | |
iterator | begin () |
returns initial iterator for the kvector | |
iterator | end () |
returns end iterator for the kvector | |
Protected Attributes | |
MYTYPE * | vptr |
pointer to current vector array | |
int | count |
current number of used slots in vector | |
int | length |
current number of slots in vector | |
Friends | |
ostream & | operator<< (ostream &os, kvector< MYTYPE > &k) |
overloaded output operator |
We are making a vector class. A vector is like an array but the length can vary. We will use templates so that it can work with an array of any type and make it longer as needed. This is similar to the way the Vector class from the STL works, but much simpler. I have also added some other methods and operators to better support the Vector data type.
Definition at line 31 of file kvector.cpp.
|
default constructor
Definition at line 151 of file kvector.cpp. References kvector< MYTYPE >::count, DEFSIZE, kvector< MYTYPE >::length, and kvector< MYTYPE >::vptr.
|
|
set initial vector size to size
Definition at line 162 of file kvector.cpp. References kvector< MYTYPE >::count, kvector< MYTYPE >::length, and kvector< MYTYPE >::vptr.
|
|
Definition at line 35 of file kvector.cpp. References kvector< MYTYPE >::vptr.
|
|
access the access function can be used on either side of the = since it returns a reference to the array member being accessed. Definition at line 45 of file kvector.cpp. References kvector< MYTYPE >::length, and kvector< MYTYPE >::vptr. Referenced by vector_float(), and vector_int().
|
|
adds the integer x to the next slot in the vector if the vector is full, it allocates a new array, copies the values, and adjusts the pointers count is the number of used slots length is the number of slots if the count and length are equal, then the array is full and we need to allocate a new bigger array. We make the array bigger in chunks to avoid having to run this code too often. after the new array is created, we copy the old array elements to the new array. Changing the internal kvector array pointer (vptr) to point to the new array completes the work. We can delete the old array now. length is changed to the new array size. Whether or not the array was extended, we add the parameter x to the array. Definition at line 190 of file kvector.cpp. References kvector< MYTYPE >::count, INCR, kvector< MYTYPE >::length, and kvector< MYTYPE >::vptr. Referenced by vector_float(), and vector_int().
00191 { 00192 /*! count is the number of used slots 00193 length is the number of slots 00194 if the count and length are equal, then the array 00195 is full and we need to allocate a new bigger array. 00196 We make the array bigger in chunks to avoid having to run this code 00197 too often. 00198 after the new array is created, we copy the old array elements 00199 to the new array. 00200 Changing the internal kvector array pointer (vptr) to 00201 point to the new array completes the work. 00202 We can delete the old array now. 00203 length is changed to the new array size. 00204 Whether or not the array was extended, we add the 00205 parameter x to the array. 00206 */ 00207 if( count == length) { // make new array 00208 cout << "making new array" << endl; 00209 MYTYPE * tptr = new MYTYPE[length + INCR]; 00210 if(tptr == NULL) { // this is fatal 00211 cerr << "No memory for new array" << endl; 00212 throw ("no memory"); 00213 } 00214 // copy the old array elements to the new array. 00215 for(int i=0; i < count; i++) tptr[i] = vptr[i]; 00216 // delete vptr; // we don't need this anymore 00217 vptr = tptr; // make the new array the current array 00218 length += INCR; 00219 } 00220 vptr[count++] = x; 00221 } // add |
|
returns initial iterator for the kvector this creates a new iterator object and initializes it to the beginning of the underlying array (vptr). It then returns the iterator. Note that x is a pointer because new returns a pointer, but we have to return the actual iterator object because that is what is expected of begin(). Definition at line 125 of file kvector.cpp. References kvector< MYTYPE >::vptr. Referenced by vector_float(), and vector_int().
00125 { 00126 iterator *x = new iterator(vptr); 00127 return *x; 00128 } |
|
returns end iterator for the kvector This also creates a new iterator and points it at the memory just after the end of the array. it does this by getting the address of the last cell in the array &vptr[length-1] is the address of the last element in the array Then we add 1 to get the address of the next place after the end of the array. Definition at line 138 of file kvector.cpp. References kvector< MYTYPE >::length, and kvector< MYTYPE >::vptr. Referenced by vector_float(), and vector_int().
00138 { 00139 iterator *x = new iterator( (MYTYPE *)(&vptr[length-1]) + 1); 00140 return *x; 00141 } |
|
Definition at line 39 of file kvector.cpp. References kvector< MYTYPE >::length. Referenced by vector_float(), and vector_int().
00039 { return length;} |
|
overloaded array access operator makes our vector look like an array since we can access the elements using the [] operator. It also checks that we are not trying to access something we shouldn't. Note that it is very similar to the access() method Definition at line 59 of file kvector.cpp. References kvector< MYTYPE >::length, and kvector< MYTYPE >::vptr.
|
|
print the current data elements in the vector
Definition at line 174 of file kvector.cpp. References kvector< MYTYPE >::count, and kvector< MYTYPE >::vptr. Referenced by vector_float(), and vector_int().
|
|
overloaded output operator prints the contents in a line. Assumes the base type (MYTYPE) has a << operator defined. Definition at line 71 of file kvector.cpp.
00072 { 00073 for(int i=0; i < k.get_length(); i++) 00074 os << k[i] << " "; 00075 os << endl; 00076 return os; 00077 } |
|
current number of used slots in vector
Definition at line 145 of file kvector.cpp. Referenced by kvector< MYTYPE >::add(), kvector< MYTYPE >::kvector(), and kvector< MYTYPE >::show(). |
|
current number of slots in vector
Definition at line 146 of file kvector.cpp. Referenced by kvector< MYTYPE >::access(), kvector< MYTYPE >::add(), kvector< MYTYPE >::end(), kvector< MYTYPE >::get_length(), kvector< MYTYPE >::kvector(), and kvector< MYTYPE >::operator[](). |
|
pointer to current vector array
Definition at line 144 of file kvector.cpp. Referenced by kvector< MYTYPE >::access(), kvector< MYTYPE >::add(), kvector< MYTYPE >::begin(), kvector< MYTYPE >::end(), kvector< MYTYPE >::kvector(), kvector< MYTYPE >::operator[](), kvector< MYTYPE >::show(), and kvector< MYTYPE >::~kvector(). |