Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

about source code of Vector

Status
Not open for further replies.

beyond

Programmer
Jan 22, 2003
4
CA
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.
tellis.gif

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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top