Alternative for std::vector/STL?

Started by
30 comments, last by rick_appleton 17 years, 11 months ago
Hi everybody, A few months ago in one of my projects I used the std::vector class. I restarted my project and am working on small executables / 64K apps. But because of this - I lost the opportunity to use the std::vector class. Is there a possibility to include it in my projects? I like this class. It is very easy to use, I think (okay - the parts of it I need). So...Any alternatives for std::vector/STL??? thx! cu
Advertisement
Well, if your data is static, you could just use a standard c-array. If you do need the flexibility of a vector, it is pretty easy to make a simple one yourself.

The class contains a templated pointer and the size of the array. Whenever you need more space in the array, copy its contents to another array, re-new the original array to twice the size, then copy the data back. Add in a few functions to push_back and overload the [] operator and you are set...
Sean Henley [C++ Tutor]Rensselaer Polytechnic Institute
What makes you think the standard vector takes up more space than any alternative?

Stephen M. Webb
Professional Free Software Developer

I agree with Sr_Guapo, if you're making a tiny exe, then you'll most likely want to write your own vector class so you have as much control over it as possible. An array would probably result in less code, so long as it doesn't need resized often.
You should just write your own vector class, in most cases it is just a linked list. If you need any advice on how to make it just let us know.

theTroll
Quote:Original post by TheTroll
You should just write your own vector class, in most cases it is just a linked list. If you need any advice on how to make it just let us know.


He shouldn't write a vector class as a linked list, he should write a linkedlist (or list/slist as C++ calls it) class as a linked list. Also I agree if you need a small executable, then STL shouldn't be used. Your own, non-templated (templates, in most cases create code for every template class), class might be the best idea.
Quote:Original post by Bregma
What makes you think the standard vector takes up more space than any alternative?


He doesn't mean overhead from the stl-vector implementation,
but space overhead from linking with the stl - which will make
his 64kb demo bigger (eg stlport.lib is 950 kb big).

And it shouldn't be too difficult to make your own custom
vector-template which doesn't depend on anything... Again, usually
I would advice against custom vector/list/... implementation, but
for a 64kb demo I can see whe he wants to do it that way.

visit my website at www.kalmiya.com
The reason I said write a "vector" class is so that he would write a class using the same naming convntion and functions that he is used to.

theTroll
Quote:Original post by CTar
Quote:Original post by TheTroll
You should just write your own vector class, in most cases it is just a linked list. If you need any advice on how to make it just let us know.


He shouldn't write a vector class as a linked list, he should write a linkedlist (or list/slist as C++ calls it) class as a linked list. Also I agree if you need a small executable, then STL shouldn't be used. Your own, non-templated (templates, in most cases create code for every template class), class might be the best idea.

I've read about at least one implementation of the vector class which is incredibly efficient, and rather clever in my book, regarding code size. It specializes std::vector<void*>, I believe, and implements just about all of the code there. Then for every other type of std::vector, it inherits from std::vector<void*>, and the only additional code it needs to add is a few functions that contain a reinterpret_cast<>() call, which takes very little space (possibly no space?) code-wise.

If you do write your own vector class, it wouldn't hurt to look at the code for an already implemented version. You might find some useful things in there.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
Quote:Original post by TheTroll
You should just write your own vector class, in most cases it is just a linked list. If you need any advice on how to make it just let us know.

theTroll


Its my understanding that in all cases its continous memory, that's part of the spec. You can't legally implement a vector as a linked list.

Ahh, I see you clarified your point. I'll accept that logic:)

Cheers
Chris
CheersChris

This topic is closed to new replies.

Advertisement