C++ standard library - when should I use it and when shouldn't I?

前端 未结 8 545
离开以前
离开以前 2020-12-29 19:23

I was wondering how often people actually use much of the standard c++ library, particularly the stuff in the and

相关标签:
8条回答
  • 2020-12-29 19:56

    It's possible you're being lazy or stubborn. Personally, I use them all the time in production code.

    I don't do this to be fancy, and I don't do this because I like writing "space-age code." Rather, I do this because I am a paranoid programmer, and I know that production environments are hostile places that will mutilate code and reduce my programs to smoking piles of worthless bytes, if given a chance.

    I do this because I live by the motto, "The best code, is the code you never write." It takes time to learn how to use the STL & Std Lib effectively, but once you do you'll find that it can be used so that what now is 1000 lines of code becomes perhaps 100. Those 100 might take as long to write as the original 1000, but there are fewer failure points. The code can be more robust, if you stand on the shoulders of others.

    0 讨论(0)
  • 2020-12-29 19:57

    Just about everyone using C++ uses the STL, especially <algorithm>. You really don't want to have to write sorting functions yourself; you'll just end up making mistakes, and in the end the performance will probably be worse.

    Whether or not there are performance gains obviously depends on what you are comparing to. In general though, using the STL algorithms is typically faster than writing your own functions -- unless you happen to have a particular trick that applies to your use case that could give you the edge. If you just write a standard quicksort yourself, it will probably be slower than the one in the STL.

    0 讨论(0)
提交回复
热议问题