#### Archived

This topic is now archived and is closed to further replies.

# Should I be using STL?

This topic is 6447 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ok, after spending a fair amount of time recoding part of my game recently I decided to submit it to a public MS VC++ newsgroup and see if any of the programmers there were interested in stepping through my code and letting me know what their thoughts are.... The overwealming reaction was... use stl. -I''m mallocing everything myself, mostly strings. I was recommended basic_string -I use some basic C code for reading file in to a memory map. I was recommended iostream -I have a bunch of linked lists and queue. I was recommended map and queue and vector? etc the list goes on... So, from a game programmers perspective, should I be doing this? I''m willing to accept a minor speed reduction so that I dont have to spend so much time debugging. I also need my code to be threadsafe, which isn''t difficult though all i know how to use is mutex''s. So, in the opinions of game programmers should I rewrite this code again to teach myself STL. (The STL code wouldn''t be used in any graphics programming, just core game code, message handling, loaders, networking etc) thanks gimp

##### Share on other sites
i would have to say yes (if you have the time and patience to do a re-write).

i try to use STL at every opportunity.

##### Share on other sites
STL is written with speed in mind, so it shouldn''t be much slower than anything you write yourself. But you have to use the correct type for the correct purpose.

list and vector look the same, but they''re implemented differently: list is a doubly linked list, vector is an array. So if you''re going to do a lot of insertions in the middle, list is faster, but if you''re going to do a lot of random access, vector is faster.

I''d say go with STL, because the code is probably more bug free than anything you write yourself to do the same.

Erik

##### Share on other sites
The STL is C++ code, so is iostream
malloc isn''t!

DO NOT MIX malloc''s with new''s!
& demalloc with deletes

It sounds as though you created a C program, and everyone gave you C++ advice...

##### Share on other sites
What''s STL? I''ve heard the term several times on this board, and I have no clue. Thanks

##### Share on other sites
Hey!
I once wrote a linked list, and then I heard of the STL. Being rather naive then, I thought that _my_ list was goning to be faster, since it is more specialized. So, using a profiler, what did I find?
Yes, you guessed it, the STL was faster, tho not by much (how many ways can you do a linked list? ). So at that point I decided to use the STL. Me being used to C++ classes etc, the STL uses C++ stuff but it doesn''t use classes in the normal way. You see what I mean if you look at the reference (generic algorithisms (<- spelt wrong?) etc). Also using the STL with pointers is a pain in the ass.
So I did a load of wrappers around the STL, that make it simpler, but not slower - all the functions are inline and it uses templates like the STL. In fact, I am pretty proud of it - it hasn''t had one bug yet. I would make a wrapper around it, so that its simpler to use -
std::list ASurface becomes:
Tools::List > (Safe pointer automatically deals with memory management. I don''t have any memory leaks - OK not that many ). For instance, using the STL to iterate through anything while deleting pointers in that list is a pain in the ass.It took ages to learn the STL (at least for me. Maybe I''m just dumb . Oh yeah, if you have the code to the STL (i think everyone has it) you''ll see that much of the code is in assembly... pretty fast.

Happy (or not so happy) coding with the STL.

##### Share on other sites
Thanks guy''s looks like I''ll get stuck in to it then, I''ve got MSDN installed which has some pretty lame doco on STL (IMHO). Is there anything around that might be better for me? Something with examples perhaps?

Thanks

Chris

STL

YAP YFIO,

##### Share on other sites
Ahhhh.... many thanks.. I''ve been searching MSDN for a while with no example to be found... I guess they prefer MFC

##### Share on other sites
Hrrmmm.... That SGI site doesn''t refer to iostream... I guess that must outside their scope.

Could anyone offer a little code snippet that will open a text file and read it in to memory and get a few character from the data?

Or perhaps point me in the direction of some examples? I''m looking through MSDN''s doco and it has too many options to count and I''m not sure where to start....

Many thanks

Chris

##### Share on other sites
  #include #include int main( int argc, char **argv ){ if( argc < 2 ) { std::cout << "specify filename. i.e. fileTest.exe somefile" << std::endl; return 1; } std::ifstream inFile( *(++argv), std::ios::in / std::ios::out ); // The / is suppose to be an 'or' operator std:: cout << inFile.rdbuf(); return 0;}

Hopefully that's a start.

YAP YFIO,

Edited by - deadlinegrunt on June 25, 2000 10:00:10 PM

Edited by - deadlinegrunt on June 25, 2000 10:00:31 PM

Edited by - deadlinegrunt on June 25, 2000 10:01:22 PM

##### Share on other sites
Thanks it IS a start. The code needed to make STL ''do stuff'' seems a lot different to the way I code classes, perhaps thats a lesson in itself, but for now I''m just trying to make sence of it all...

In the interim I''ve been playing with map::. It looks like a nice place to hold my keyboard bindings... this might not seem so bad after all...

Thanks again...

##### Share on other sites
IMPORTANT NOTE:

The original poster seemed to insinuate that STL was suggested as a way to make things thread-safe.

If you want to make it thread-safe, you must handle synchronization externally.

On another note, it seems that about 10-20% of the questions on this board are starting to be about STL. Maybe there should be a separate discussion group for it?

##### Share on other sites
Stoffel, small comment on your post about thread-safety: first of all, technically, STL says nothing about thread-safety. An implementation does. The STL implementation from SGI (on the page that deadlinegrunt mentions, is thread safe. For a discussion, see http://www.sgi.com/Technology/STL/thread_safety.html. SGI's implementation is exception-safe as well.

Another popular (free) STL implementation is STLport (see http://www.stlport.org). Since it's based on SGI's code, it's thread safe as well. It includes the new SGI IOstreams library as well.

Other implementations (like the one that comes with VC++ 6) might not be thread safe. The best way to find out is to check the documentation that comes with it.

Erik

Edited by - Erik Post on June 26, 2000 3:23:19 PM