Should programmers use STL or write their own code? [closed]
Asked Answered
B

12

44

I don't know much about C++ data structures but I am wondering do you (programmers) use STL or write your own code? After all STL is designed for doing tasks like searching, replacing and much more through a list of data.

Someone really don't need to learn much about the linked list, binary search and many more because I could use STL. What would you suggest?

Bobbyebobbysocks answered 26/7, 2011 at 14:2 Comment(8)
Don't reinvent the wheel. This doesn't mean don't understand how the wheel works and when it the best time to use it, but STL is a well-written library and would most likely be safer/faster to use.Checkup
I suppose this question isn't that open-endedCrambo
I'd recommend trying to implement your own if you have the time.Delrosario
@Philip: Why horrible? It sounds like a legitimate, important question to ask to me?Plethoric
Possible dupe: Should I use the municiple sewage plant or try and do it myself?Pirog
Wheter or not this is a legitimate question aside, I can't believe it gets so much attention.Jonathanjonathon
@Enno Shioji That was my point. This is a high-level decision about how to start a project. The philosophy of programming. It's a good question. For programmers. But stack overflow kept this one, and sends all the bad questions to programmers. We're feeling abused over there.Brendonbrenk
I think it sgould have been at programmers @Philip, It has already got a vote for deletion now hereBobbyebobbysocks
S
121

You should use STL, because it is well tested and optimized.

That doesn't mean you shouldn't know how to write these data structures yourself. With that ability under your belt, you will be able to choose the best STL data structure for your application.

Sponson answered 26/7, 2011 at 14:4 Comment(1)
I would argue, that the accepted answer is wrong. The Standard is strictly neutral on whether to write your own container or not.Warram
S
63

While the Standard Template Library is convenient when it comes to performing tasks as you mentioned like Searching, Replacing, using Linked Lists, it should not replace the knowledge of what is going on inside of the Library.

You mentioned not needing to learn about linked lists, binary searches and "many more", however you should have at least a working knowledge of how these Data Structures and procedures work as it will make using them (and knowing when to use them) much more effective.

Basically - you don't have to reinvent the wheel, but at least know what makes the wheels effectively turn.

Stearne answered 26/7, 2011 at 14:6 Comment(0)
H
11

Use STL and standard libraries in general when you can. First it is probably way better tested than your own code, and second it helps keep your code portable. The only time you should rewrite any of this functionality is for learning purposes. It may be educational to write your own associative map or linked list, but for production code, stick with well tested and standard compliant code.

Hypotenuse answered 26/7, 2011 at 14:5 Comment(0)
C
7

A working knowledge of the underlying data structures, methods and applications of the tools provided by the STL will make you a much better programmer. Knowing when to use what container type, or which algorithm is as important as a proper implementation. Sometimes, the easiest way to understand some of the more complex concepts is to implement them yourself in the context of a data structures and algorithms class.

That said, the code in the STL has been written by experts and refined over time into a standard library that is used my millions of people world wide. In practicality, there is almost never a reason to "roll your own" except for extreme cases where performance (or size) matter to a point that is critical for your exact application.

Cilo answered 26/7, 2011 at 14:6 Comment(0)
A
3

I hesitate to write, since I haven't written C++ in 5 years. But a couple of things came to mind that haven't been discussed yet.

If the implementation is a bad fit for what you need, don't use it if you can write and test your own easier than using the library. I recently ran into this in Java, where I needed a fast bitset. Details:: There are two relevant classes in the JVM (BitSet and BigInteger). BitSet doesn't support initialization other than by setting one bit at a time; BigInteger has an irrelevant signum that confused things, and is immutable, which is costly in my case. I ended up writing my own, with tests, in a few hours. It fits better, is faster, and I can do whatever I want to it.

The other reason to write your own is if you don't understand the specification of the library implementation relative to your requirements, can't easily test it or read it to figure out what it does, and can easily roll your own. This is/was a particular problem with STL implementations that are (or at least used to be) shipped with terse, inadequate, cryptic documentation and commentless, opaque source code that rolls over your head like a huge rogue wave.

Anarchism answered 26/7, 2011 at 22:51 Comment(0)
D
3

Just to add my answer (from comment above):

Yes you should think about implementing e.g. a sequence, a linked list, using your own code.

This is what they teach on Comp Sci courses and it is not without good reason. But, if you're looking to work quickly then just use STL.

I just think people should understand how the tools really work.

Delrosario answered 27/7, 2011 at 3:27 Comment(2)
Actually, "introduction to data structures" classes make you learn about (and implement) these structures so that you can choose the best one for the job at hand. Choosing a data structure with the wrong algorithmic complexity can be disastrous. Plus, some problems require clever variations of well-known data structures. Since they're not of general interest, they're probably not supplied by a general purpose library.Jonathanjonathon
The only way you can find out about how things like time complexity relate to data structures is by experimenting. I guess then that the STL is always going to be useful for building blocks of programs.Delrosario
U
1

I use the C/C++ Standard library and STL because it is a really big time saver and I don't see the need to reinvent the wheel. I also use boost where I can.

It is still a good learning exercise to write your own container class and algorithms.

Uncommunicative answered 26/7, 2011 at 14:6 Comment(0)
Z
1

Use STL unless you have a compelling reason, e.g. speed or correctness, not to. Definitely know how to write basic data structures on your own.

Zoospore answered 26/7, 2011 at 14:6 Comment(4)
correctness! Are you implying their are bugs?Greenbelt
No, just that there doesn't exist a data structure/algorithm in the STL to correctly solve every problem you can come up with. If your problem is such that you can't find a way to couch it in terms of the STL, writing your own code might be the only option.Zoospore
@Martin: Are you implying that there is an STL implementation that is 100% bug-free?Boustrophedon
@Joe Wreschnig: Depends which version. But I am willing to bet that any major version is bug free (or has the bugs well documented). They are so heavily used and tested that any bugs would cause a lot of chatter. They are over 10 years old and relatively simple. So yes I am willing to use any STL implementation as if it was 100% bug free.Greenbelt
O
1

In general you should use STL or Boost containers because of their effectiveness and reliability. Still you need to have a corresponding world view on existing containers. You should know its con and pros. Studying of the container internal structure and working principles allows you to reach better understanding.

Outland answered 26/7, 2011 at 14:8 Comment(0)
C
1

STL is well tested and reliable but it is not the fastest solution. Some of the containers allocate a lot of small memory blocks rather than one big block. If you really have a speed problem then you may consider making your own fixed-size list. But for many purposes STL is the standard solution.

Coalition answered 26/7, 2011 at 14:12 Comment(6)
"Some of the containers allocate a lot of small memory blocks rather than one big block." -- std::vector doesn't, std::array doesn't. What advantage is there to creating your own fixed size list when you can use either of those?Coreen
All the containers allow you to specialize the allocators. The default ones are very fast and will probably beat anything you can write in an attempt to write a faster version. Bad advice in my opinion.Greenbelt
@Martin: The STL allocator model is terrible. How do I create a node pool for std::list? There's no portable way to do it because std::list does not expose the size of the nodes it allocates. Anyone that actually cares a lot about performance writes their own containers with a better allocator model.Powers
Any general purpose library will fail to be performant under some special case. But since the original question gives no constraints whatsoever, it doesn't make sense to recommend writing custom containers.Establishmentarian
@Peter Alexander: I don't think it is that horrible (but I am sure there are situations they do not work optimally). But I do some pretty computationally intensive stuff and I have only once had the containers be the bottleneck in my code requiring a custom allocator that easily sorted the problem. I would like to bet the standard container will beat most beginners in efficiency.Greenbelt
Replace "some of the containers" with "node-based containers": std::list (linked list), std::set and std::map (red-black trees). I don't see how you can implement a linked list or binary search tree without allocation of "many smal memory blocks". And I don't see this as a disadvantage. When you need to splice elements from a list, you can't use a vector.Jonathanjonathon
K
1

Very smart people wrote the STL. Then more very smart people have used it, tested it, and refined it. You think you are going to do better? Rarely. It is a great tool; you should use it whenever possible!

Kickoff answered 26/7, 2011 at 21:20 Comment(0)
P
0

Write low-level stuff yourself when learning/playing, but at work use code that was written and refined by experts over years, and has/is being tested by thousands of engineers in thousands of different conditions.

In other words, use your code only if your code can beat the other codes!

Plethoric answered 27/7, 2011 at 2:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.