STL vs your own

Started by
96 comments, last by doodah2001 22 years, 2 months ago
I''m just curious as to how many people use the STL for such things as lists, maps, and vectors vs. creating their own customized list or vector class and I was also wondering why ya''ll choose one type over the other. Thanks.
MatDoodah2001@hotmail.comLife is only as fun as you make it!!!
Advertisement
I use the STL because it works. I''ve written about a zillion linked list implementations in my life and don''t ever want to again

Actually, I think everyone should write there own list classes at least once in their lifetime. Write a single linked list, doubly linked list, array, and hash table class, and use it in a project. It''s good practice and you can really see some of the time/space tradeoffs.
I use the STL containers almost exclusively. They have many great features and chances are that even if you''re a professional programmer, the STL is probably written better than what you would right, especially if you wanted all of the features the STL provides.

I''ve seen lots of tests showing that the STL containers are just as fast as their non-STL counterparts (i.e. vector<> is as fast as an array).

I would say that the only time you need to write your own data structure is when you need a feature that the STL does not provide for you.

Russell
Just why would you waste time to reimplement something that already exists and works ?

As to choosing vector or list. Well, unless you REALLY need to be able to insert elements in the middle of your structure, go for the vector.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
I use the standard C++ library unless I can''t (e.g. kernel-mode code). That''s pretty much the only reason not to use a standard class -- assuming, of course, that an appropriate standard class exists. Oh, I also use the various hashed containers that many library implementations have.

The well-defined interfaces and thoroughly tested code are a boon.

char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
I prefer to use home-made classes, mainly for 2 reasons:
1- STL syntax is crappy
2- I can customize my own classes to do exactly what i want them to do, and to be optimized for the use i have. For example, in the game im working on, I always access the first, same or next element of a list, so I''ve build a simple multi- linked list that garanteed those 3 access operations to be done in O(1).



quote:For example, in the game im working on, I always access the first, same or next element of a list, so I''ve build a simple multi- linked list that garanteed those 3 access operations to be done in O(1).

What the hell are you on about?

Accessing the next, previous, or same item in a list is a constant-time operation anyway.

@_@.




As for which container to pick, it''s almost always obvious, because the containers all have different properties.

char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
I use the STL because it works and its fast (most STL implementations are extremely efficient, so unless you have ridiculously specific needs for containers/iterators/string class/etc you''re unlikely to write code that surpasses their efficiency without putting in a ton of work, years of debuggin, etc).

It is somewhat annoying that it doesnt fit with my standard syntax usage (I tend to use upper-case-first class names like Vector..not vector), but no biggie; that''s always a potential issue with any API.

...

I second OldGuy''s claim that people should try implementing the classic data structures and algorithms STL abstracts just to be sure they know what''s going on ''underneath the hood''. In most cases, however, once you get them working I think you should just toss them out and go with the STL implementations.
STL all the way
But, I would suggest to anyone who doesn't know how to code a linked list, stack, queue, hash table, etc. I have created my own classes for those abstract data types at one time or another. It is a GREAT learning experience, so do it!!

Good Luck!

EDIT: Forgot to say why I use STL mainly for:

  • It's thoroughly tested

  • It's probably more optimized than the ones I have made

  • It's a standard

  • Very robust

  • It fits my needs

  • I don't want to completely reinvent the wheel; just learn how it works



Edited by - Floppy on January 31, 2002 10:51:30 PM
I prefer to not use STL.

Its naming convention completely conflicts with my normal one, and writing a wrapper to get around this would be a lot of overkill for little gain. But that''s not the main reason.

The main reason is that I prefer to have simple data structure templates which, while not necessarily QUITE as optimal performance-wise as STL, are more tailored to my needs on all levels, including interface, behavior, and storage. This tailoring means I can create data structure classes which are lightweight, being only as complex as I need them to be and nothing more.

In my case, the small speed gains and increased "standardness" of moving over to the STL are not worth the sacrifice I incur in terms of control. Working with third-party libraries for certain pieces of functionality is one thing, and when such libraries serve a specific problem domain and have a solid well-defined interface, I wholeheartedly endorse that. But the STL falls into the land of glue code, and that''s a completely different story as far as I''m concerned.

The STL is a very powerful and very efficient template library, but it''s a massive thing since it''s intended to be all things to all people. In contrast, I use a set of lightweight data structures that are entirely contained in a single header of less than 1000 lines, they perform decently and I have complete control over them. They fit my code better than the STL does, and that''s what matters.

To put it another way, yes reinventing the wheel is generally a bad idea, but it''s also a bad idea to use a truck tire when you don''t own a truck.

I don''t want to turn this into a religious war, since I''m fine with people either choosing to use the STL or choosing not to use it; everyone has their reasons. So by the same token, it''d be really nice if some people on this board (and elsewhere) would stop treating anyone who deliberately chooses not to use the STL as if they were somehow inferior.

Library choice, like language choice, is up to personal preference.

- Chris

This topic is closed to new replies.

Advertisement