Preferring STL algorithms from algorithm and numeric
thecfamilia.wordpress.com/2014/04/20/preferri...Posted Apr 20, 2014, 3:14 PM UTC
Posted Apr 20, 2014, 3:14 PM UTC
As much as I love STL algorithms, I have to admit that I don't love typing thing.begin(), thing.end() all the damned time. I like adding a , 0 even less. I've been known to write wrapper functions that just take a container and pass begin and end along to a standard algorithm.
On the other hand, I do appreciate being able to specify the accumulate type, because there are times I'd like to add my floats using a double to get more precision.
Can't you use Boost.Range ?
auto sum = boost::accumulate(vector, 0, plus<>);
I prefer begin(thing) and end(thing), they're more general.
As a fan of STL I really enjoyed the article.
One side note, I think we should finally start phasing out this narrative: "lol, C bad, unsafe, here's how we do it in superior C++". It was good 20 years ago, but these days most people either jump straight to C++ or come from languages other than C (like Java). This narrative not only reinforces the harmful notion of C++ being "just a better C", but also makes us miss an opportunity to confront C++ with other modern languages often sold as better than C++ or even obsoleting it. Notice for example how you could easily use Go as a bad boy in the article, instead of C.
I'm currently a student and I see students writing C like in the example code. It may be more relevant than you think.
Yeah. My school teaches C++ like it's C with classes. WTF.
Thanks a lot for reading the article! The C code that I displayed was code that I see in C style C++; granted, things are miles ahead now of how it was in 1998, and I'm so glad to see the new standard embraced by newcomers to the language, but unfortunately the sample code I listed happens quite often.
But yet, even today I was explaining how some particular code has no type safety, because the interface is old C. The earliest copyrights in the code date back to 1983. We still have much of the interface like this:
foobar(int opcode, void * data);
That data might be being sent into foobar, it might be returned from foobar. It might be a global parameter structure, it might be a struct with data for a single call. We should rewrite the interface ... except we'd still have to support the old interface because our flagship product calls it.
Nice but incomplete. Shouldn't you actually implement some code using accumulate. Maybe I missed it as I started skimming after realizing that the article goes on forever.
C UR DOIN IT RONG
double mean(double *data, size_t len)
Now that’s some really nice, concise, readable code
Uh, not as displayed by my browser. Maybe you'd like to web-ize that code. It looks copy-pasted from your editor, and HTML eats whitespace for lunch.
template<typename ForwardIterator,
typename T
> T mean(ForwardIterator first, ForwardIterator last, T start)
{
return sum(first, last, start) / std::distance(first, last);
}
Looks awful, doesn't it? Let's try a few purely stylistic changes.
template<typename ForwardIterator,
typename T>
T mean(ForwardIterator first, ForwardIterator last, T start)
{
return sum(first, last, start) / std::distance(first, last);
}
Now that is readable code in my opinion.
Really? Sorry, I tested it on Firefox and it looked fine. The second angled bracket on a line by itself before the T is my crappy personal style though, I should go back to doing it the more civilized way.
You might like to read through the JSF code standards document.
[deleted]
There are a lot of general purpose, generic algorithms in the algorithm and numeric headers, and you should always, always, always, always, (always!) use them over your own implementation.
I'd wager that Alex Stepanov would not agree with this ;)
Why?
Well, since I've (rightly) been downvoted for being vague and wry, i'll link you to a ~50 minute discussion by Alex himself on why the STL, as is, has flaws. I won't tl;dr because it's worth watching, as are the next few on similar kinks in the STL.
pasting stepanovs lecture as a link evidence to your argument should be a crime... cuz nobody is gonna listen to him talk 50 min to say 3 sentences worth of content.
Very nice post. However I think you need forward iterators for your mean function. Once you've "read" from the input iterators to compute
sumyou're not guaranteed to be able to access the same elements a second time to computestd::distance. For example, if I seemeantakes input iterators as parameters, I assume that I can pass instd::istream_iterator's, but it doesn't seem like that will work.Gosh, you're right. I'll get that fixed as soon as I get home. Thanks for the read!
No problem, I enjoyed it! It's a message that I've been telling students at my university for a few years now (ex C++ TA.)
Note that the correct fix isn't to require forward iterators, it's to count while you're iterating.
Note that you can still use
accumulate, you just need to use a pair type to contain the count and sum.I think both fixes can be correct, depending on what the expectation is. If all you need is a single function to compute the mean, and sum is simply an auxiliary, then yes input iterators + single pass is the way to go. However if you're expecting to compose the two algorithms and have them exist as standalones, then you need to step up the requirements to forward iterators. Correct, for varying definitions of "correct" :-)
Edit: I'm not trying to side for or against any one solution. Just offering a different perspective. I'm a big fan, btw :-)