Use STL or reenvent the wheel?
hi,
I have some kind of "psychological" problem when programming C/C++ and I need your help to eliminate it :) :
When I need a container like a linked-list or a hashmap I always tend to start writing it from scratch. I don't know why I do it, (in Java i.e. I always use the API's container classes) maybe I'm afraid that STL is too slow. I need your opinion when to use STL (by the way, is there a alternative to STL?) and when to write the container code myself.
bye
Chris
p.s. What is the most recent STL implementation people use?
By all means, write your own linked lists and containers for the learning experience, but remember STL is your friend. It is well designed code, and is made for maximum performance and functionality. STL isn’t slow, as long as you use it properly. I'll try and find some resources on working with STL for you.
Update: ok, here are a few tutorials i found:
templates
vector & deque
list, set, multiset
map, multimap, hash_map, hash_multimap, hash_set, hash_multiset
Container adaptors
The source site can be found here. Hope this helps you.
Update: ok, here are a few tutorials i found:
templates
vector & deque
list, set, multiset
map, multimap, hash_map, hash_multimap, hash_set, hash_multiset
Container adaptors
The source site can be found here. Hope this helps you.
vector is your friend :) Use it efficiently and it is certainly fast enough. I use it for nearly all of my dynamic lists now, it has really helped to clean up my code.
The STL is written by very intelligent people. Consequently, the code written is fast. Not only is it fast, it's also the standard, which means other programmers will be able to know exactly what your code is doing. Furthermore, it's already there waiting to be used by you, so reinventing the wheel seems rather pointless. It is also good to know how the STL works so that if you gain employment and are expected to use it, you can without looking a little embarrased. Boost is a great addition to the STL, but maybe not a replacement.
At the moment, I am using the VC++2005 Beta 2 set of STL files.
At the moment, I am using the VC++2005 Beta 2 set of STL files.
Are you trying to learn about containers? Write your own.
Are you trying to do something else? Use STL.
Those two basic guidelines will hold true in most situations, until you get a good grasp of the performance of STL. From there, if you need something that has to be optimized to a certain type, or to do a special something, then its possible that you'll stop using STL and write your own, optimized, super-container. But its likely that that won't happen for a while, if at all.
Are you trying to do something else? Use STL.
Those two basic guidelines will hold true in most situations, until you get a good grasp of the performance of STL. From there, if you need something that has to be optimized to a certain type, or to do a special something, then its possible that you'll stop using STL and write your own, optimized, super-container. But its likely that that won't happen for a while, if at all.
Wow, thanks for so many replies in so little time!
Is there a recent free implementation of STL?
Is there a recent free implementation of STL?
Quote:Original post by Zoomby
Wow, thanks for so many replies in so little time!
Is there a recent free implementation of STL?
C++ compilers come with an implementation of the standard library containers & algorithms. If you are using something old like VC++ 6.0 (it has poor standard compliance & a poor imp of STL) i suggest you get a modern C++ compiler (like VC++ 7.1 or 8.0). If you do need to get an imp you could use STLport
Quote:Original post by Zoomby
hi,
I have some kind of "psychological" problem when programming C/C++ and I need your help to eliminate it :) :
When I need a container like a linked-list or a hashmap I always tend to start writing it from scratch. I don't know why I do it, (in Java i.e. I always use the API's container classes) maybe I'm afraid that STL is too slow.
Really, the STL SC++L (What was previously known as the Standard Template Library is now a part of the Standard C++ Library) is quite fast at what it does. I should point out that there is currently no hashed map in the current version of the standard, although it might be is part of TR1 as std::tr1::unordered_(multi)map/set (thanks snk_kid^).
Quote:I need your opinion when to use STL (by the way, is there a alternative to STL?) and when to write the container code myself.
Use the standard library by default. If you notice your program is operating too slowly, use a profiler. Refactor your code (prehaps switching to a more appropriate container) as needed - most of the time a custom allocator or more appropriate container/usage pattern will suffice. You'll then be able to devote all that time you'll have saved by not reimplementing basic features once more to optimizing that last 1% of circumstances where a custom container would be better than one of the standard ones.
I don't think anybody's up and coded a widely used bonified alternative, although there are many complimentary container libraries which fill gaps left by the standard ones. The Boost C++ Libraries come to mind:
Boost.MultiArray (for N-dimentional arrays, std::vector is merely 1D)
Boost.MultiIndex (for N-dimentional not-quite-arrays, std::set is merely 1D)
Boost.Array (a container-like interface for static sized arrays)
Boost.Pointer Container (for polymorphic storage - shadows standard library versions)
Quote:p.s. What is the most recent STL implementation people use?
The one that came with my compiler - even VS6 (a terribly outdated piece of ****) came with an implementation (albiet a shoddy one) of the Standard C++ Library.
That said, some people like to use a replacement version such as STLport, which is supposedly better for VS6 than the version that came with it. By which I mean that's what everyone says (including the aforementioned snk_kid) but havn't tested myself, because I havn't dared touch VS6 with a 10 foot pole.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement