When should the STL algorithms be used instead of

2020-05-21 08:47发布

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

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.

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?

13条回答
The star\"
2楼-- · 2020-05-21 09:02

Are they used more frequently than it seems?

I've never seen them used; except in books. Maybe they're used in the implementation of the STL itself. Maybe they'll become more used because easier to use (see for example Lambda functions and expressions), or even become obsoleted (see for example the Range-based for-loop), in the next version of C++ .

查看更多
Lonely孤独者°
3楼-- · 2020-05-21 09:04

The main problem with STL algorithms until now was that, even though the algorithm call itself is clearer, defining the functors that you'd need to pass to them would make your code longer and more complex, due to the way the language forced you to do it. C++ 0x is expected to change that, with its support for lambda expressions.

I've been using STL heavily for the past 6 years and although I tried to use STL algorithms anywhere I could, in most instances it would make my code more obscure, so I got back to a simple loop. Now with C++ 0x is the opposite, the code seems to always look simpler with them.

The problem is that by now C++ 0x support is still limited to a few compilers, even because the standard is not completely finished yet. So probably we will have to wait a few years to really see widespread use of STL algorithms in production code.

查看更多
一纸荒年 Trace。
4楼-- · 2020-05-21 09:10

You've gotten a number of answers already, but I can't really agree with any of them. A few come fairly close to the mark, but fail to mention the crucial point (IMO, of course).

At least to me, the crucial point is quite simple: you should use the standard algorithms when they help clarify the code you're writing.

It's really that simple. In some cases, what you're doing would require an arcane invocation using std::bind1st and std::mem_fun_ref (or something on that order) that's extremely dense and opaque, where a for loop would be almost trivially simple and straightforward. In such a case, go ahead and use the for loop.

If there is no standard algorithm that does what you want, take some care and look again -- you'll often have missed something that really will do what you want (one place that's often missed: the algorithms in <numeric> are often useful for non-numeric uses). Having looked a couple of times, and confirmed that there's really not a standard algorithm to do what you want, instead of writing that for loop (or whatever) inline, consider writing an generic algorithm to do what you need done. If you're using it one place, there's a pretty good chance you can use it two or three more, at which point it can be a big win in clarity.

Writing generic algorithms isn't all that hard -- in fact, it's often almost no extra work compared to writing a loop inline, so even if you can only use it twice you're already saving a little bit of work, even if you ignore the improvement in the code's readability and clarity.

查看更多
ゆ 、 Hurt°
5楼-- · 2020-05-21 09:11

I write performance critical applications. These are the kinds of things that need to process millions of pieces of information in as fast a time as possible. I wouldn't be able to do some of the things that I do now if it weren't for STL. Use them always.

查看更多
倾城 Initia
6楼-- · 2020-05-21 09:13

I would not use STL in two cases:

  1. When STL is not designed for your task. STL is nearly the best for general purposes. However, for specific applications STL may not always be the best. For example, in one of my programs, I need a huge hash table while STL/tr1's hashmap equivalence take too much memory.

  2. When you are learning algorithms. I am one of the few who enjoy reinventing the wheels and learn a lot in this process. For that program, I reimplemented a hash table. It really took me a lot of time, but in the end all the efforts paid off. I have learned many things that greatly benefit my future career as a programmer.

查看更多
▲ chillily
7楼-- · 2020-05-21 09:14

There are many good algorithms besides stuff like std::foreach.

However there are lots of non-trivial and very useful algorithms:

  • Sorting: std::sort, std::upper_bound, std::lower_bound, std::binary_search
  • Min/Max std::max, std::min, std::partition, std::min_element, std::max_element
  • Search like std::find, std::find_first_of etc.

And many others.

Algorithms like std::transform are much useful with C++0x lambda expressions or stuff like boost::lambda or boost::bind

查看更多
登录 后发表回答