SimpleVector is a very simple C++ template class for implementing one-dimensional vectors of small objects. It is similar in functionality to the STL vector class, which is no longer supplied with CodeWarrior. It even shares some of the same method names. But unlike the <vector.h> shipped with CW11, this one does not include ten gazillion ANSI files, and does not require extreme gyrations on the part of the programmer. You just use it.
Important Update: a potentially dangerous (memory-leaking) bug in deleteIdx() was fixed on 5/04/98. Update your projects immediately!
Here's a sample code snippet that illustrates some uses. The methods are defined below.
SimpleVectorvec; // create an empty vector vec.mBlockItems = 5; // allocate in blocks of 5 items vec.push_back('a'); // add some items vec.push_back('b'); mychar = vec[0]; // get some items mychar = pop_back(); // get last item and remove it // iterate over the list for (short i=0; i<vec.size(); i++) { DoSomethingWith( vec[i] ); } mychar.deleteAll(); // empty the list
SimpleVectorThe first format above creates a vector, whose elements are of type "myType", without actually allocating any memory. The second form preallocates enough room for 20 elements.myVector; SimpleVector myPresizedVector(20);
As elements are added, a buffer is created in blocks of mBlockItems, a member of each vector which currently defaults to 16. You can change how big the block size is by, e.g., myVector.mBlockItems = 20 (allocate room for 20 items at a time).
Note that you should never keep the address of an array element, since it might get moved around when you add or delete other items. (For the same reason, don't build a SimpleVector of certain reference- counted objects which rely on staying put.)
vec.deleteAll() deletes all elements in vec, and frees the buffer completely.