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?

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
munish
  • 4,505
  • 14
  • 53
  • 83
  • 16
    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. – Nicholas Jul 26 '11 at 17:52
  • 1
    I suppose this question isn't that open-ended – prusswan Jul 26 '11 at 19:34
  • I'd recommend trying to implement your own if you have the time. – Alex Jul 26 '11 at 23:28
  • 1
    @Philip: Why horrible? It sounds like a legitimate, important question to ask to me? – Enno Shioji Jul 27 '11 at 02:27
  • 3
    Possible dupe: Should I use the municiple sewage plant or try and do it myself? – Martin Beckett Jul 27 '11 at 02:52
  • Wheter or not this is a legitimate question aside, I can't believe it gets so much attention. – André Caron Jul 27 '11 at 03:35
  • 1
    @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. – Philip Jul 27 '11 at 13:46
  • I think it sgould have been at programmers @Philip, It has already got a vote for deletion now here – munish Jul 27 '11 at 16:05

12 Answers12

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.

Starkey
  • 9,673
  • 6
  • 31
  • 51
  • I would argue, that the accepted answer is wrong. The Standard is strictly neutral on whether to write your own container or not. – user1095108 Dec 07 '21 at 12:36
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.

Rion Williams
  • 74,820
  • 37
  • 200
  • 327
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.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
harald
  • 5,976
  • 1
  • 24
  • 41
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.

Chad
  • 18,706
  • 4
  • 46
  • 63
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.

Community
  • 1
  • 1
Ed Staub
  • 15,480
  • 3
  • 61
  • 91
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.

Alex
  • 4,844
  • 7
  • 44
  • 58
  • 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. – André Caron Jul 27 '11 at 03:47
  • 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. – Alex Jul 27 '11 at 04:03
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.

trenki
  • 7,133
  • 7
  • 49
  • 61
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.

Patrick87
  • 27,682
  • 3
  • 38
  • 73
  • correctness! Are you implying their are bugs? – Martin York Jul 26 '11 at 14:53
  • 4
    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. – Patrick87 Jul 26 '11 at 14:54
  • @Martin: Are you implying that there is an STL implementation that is 100% bug-free? –  Jul 26 '11 at 19:03
  • @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. – Martin York Jul 26 '11 at 19:31
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.

eugene_che
  • 1,997
  • 12
  • 12
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.

A Fog
  • 4,360
  • 1
  • 30
  • 32
  • "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? – Benjamin Lindley Jul 26 '11 at 14:19
  • 7
    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. – Martin York Jul 26 '11 at 14:57
  • 1
    @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. – Peter Alexander Jul 26 '11 at 19:37
  • 1
    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. – benzado Jul 26 '11 at 20:20
  • @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. – Martin York Jul 26 '11 at 21:40
  • 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. – André Caron Jul 27 '11 at 03:43
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!

ncmathsadist
  • 4,684
  • 3
  • 29
  • 45
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!

Enno Shioji
  • 26,542
  • 13
  • 70
  • 109