I need to implement a C++ class which is similar to the STL Vector. Could anyone tell me where I can get the source code of the STL Vector, e.g the URL of the source code? So that I can take it as a reference. Thanks a lot.
do a search on your system for "vector.h" or just "vector" and see what comes up. Because it's a generic class the whole thing should be declared in a viewable header file.
programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.
It's just a dynamic array.... You'll find similar code doing a web search for a "bag" class, or "Dynamic Array"... But looking at a alread implimented dynamic array is a cop out. Just remember:
Gang of Three
Constructor - nothing particularly intresting
Copy constructor - Must copy all the data over from passed array, clean up the old, and point to the new.
Destructor - Clean up.
Insertion and removal
Inserting - If null, create a new array, capacity and size one. If you have the capacity, add the item to the end. If you don't copy all the elements into a new dynamicly allocated array (double if not enough is the standard, but not neededly the implementation you'll need) and clean up the old memory.
Removing - Decriment size... Allow the elemnt at array[size] to get over written in the tird if above.
Transversing
Subscript- Check to make sure that the passed index is not bellow 0, or >= size. else return the element (by reference.
Iterators- There are two approches, internal iterator... which allows ONE way to traverse the array; or external itterators (like that of the vecotr) which are a nested class to the container, or could even be an external class.
The issue of transversal is quite an intresting one, the external iterator implimenting a genaric interface is most common, because it works... The down side is that it is a little harder to write... But the payoff is better, for the class is more powerful.
Templates, ah, templates... You'll want to start writting it with a simple object, and after it is working with (say ints) that object, make the switch to the template.
The vector libraries header is listed in the vector man page, and gives a pretty detailed explaination of how it all works, so it shouldn't be to hard to write it without the code to copy from.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.