C++ 标准库 - 我应该什么时候使用它,什么时候不应该使用它?

2022-01-11 00:00:00 algorithm header c++

我想知道人们实际使用大量标准 c++ 库的频率,尤其是 标头中的内容.教科书似乎推荐它们,但我还没有看到它们中的任何一个在我筛选过的各种项目中使用过(巧合?)而且就个人而言,每次自己编写适当的简单算法似乎更容易,而不是记忆或每次参考这些标题.只是懒惰还是固执?使用这些库时是否真的有性能提升等?

I was wondering how often people actually use much of the standard c++ library, particularly the stuff in the <algorithm> and <numeric> headers. The text books seem to recommend them, but I haven't seen any of them used at all in various projects I've sifted through (coincidence?) and personally it seems easier to just write appropriate simple algorithms myself each time rather than memorize or consult a reference to these headers each time. Am just being lazy or stubborn? Is there actually performance gains etc when using these libraries?

谢谢,

R

推荐答案

你可能是懒惰或固执.就个人而言,我一直在生产代码中使用它们.

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.

我这样做是因为我的座右铭是:最好的代码,是你永远不会写的代码."学习如何使用 STL &Std Lib 有效,但是一旦你这样做了,你会发现它可以被使用,所以现在的 1000 行代码可能变成了 100 行.这 100 行可能需要与原始 1000 行一样长的时间来编写,但故障点较少.如果你站在别人的肩膀上,代码会更健壮.

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.

相关文章