什么时候应该使用STL算法而不是使用自己的算法? [英] When should the STL algorithms be used instead of using your own?

查看:202
本文介绍了什么时候应该使用STL算法而不是使用自己的算法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常使用STL容器,但从未使用过与STL容器一起使用的STL算法。

I frequently use the STL containers but have never used the STL algorithms that are to be used with the STL containers.

使用STL算法的一个好处是,它们提供了一种删除循环的方法,从而降低了代码逻辑的复杂性。我在这里没有列出其他好处。

One benefit of using the STL algorithms is that they provide a method for removing loops so that code logic complexity is reduced. There are other benefits that I won't list here.

我从未见过使用STL算法的C ++代码。从网页文章中的示例代码到开源项目,我还没有看到它们的用途。

I have never seen C++ code that uses the STL algorithms. From sample code within web page articles to open source projects, I haven't seen their use.

使用频率是否比看起来高?

Are they used more frequently than it seems?

推荐答案

短答案:总是。

长答案:总是。那就是他们的目的。它们针对与STL容器一起使用进行了优化,并且比您自己编写的任何内容都更快,更清晰,更惯用。您应该考虑自己滚动的唯一情况是,如果您可以表达STL算法不能满足的非常特定的,关键任务需求。

Long answer: Always. That's what they are there for. They're optimized for use with STL containers, and they're faster, clearer, and more idiomatic than anything you can write yourself. The only situation you should consider rolling your own is if you can articulate a very specific, mission-critical need that the STL algorithms don't satisfy.

编辑后添加: (好吧,并非总是真的真的,但是如果您必须询问是否应该使用STL,答案是是。)

Edited to add: (Okay, so not really really always, but if you have to ask whether you should use STL, the answer is "yes".)

这篇关于什么时候应该使用STL算法而不是使用自己的算法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆