TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Manipulating C++ Containers Functionally

35 pointsby splinterofchaosover 12 years ago

4 comments

Aardwolfover 12 years ago
I do agree that C++'s STL is sometimes too verbose.<p>It's important to be generic, but it'd have been great if the most common conveniences were added as well to make it pleasant to use. E.g.: erase remove idiom, count but no contains, specifying begin() and end() even when you mean the whole container, ... Anything where you need to type the variable name of your container more than once to do one operation on it.<p>Not just the containers and stl algorithms have that annoyance, but also the string stream operators to convert numbers to strings, this is way too verbose imho.<p>At least the new initializer lists make some things way more compact and convenient now :)
评论 #4931096 未加载
评论 #4930076 未加载
bcoatesover 12 years ago
I think going this way is going to turn out painful. The sort of immutable container logic that people associate with FP is against the nature of C++ and you'll be constantly bumping into other people's code, and parts of the library, that don't like it.<p>C++ style prefers<p><pre><code> for_each(xs.begin(), xs.end(), f); </code></pre> to<p><pre><code> xs = std::transform(xs,f); </code></pre> Because the first does not copy the container and its elements, which in C++ is a meaningful operation which may not be allowed for xs's elements, or may be remarkably expensive.<p>I thought the annoying verbosity when using the whole container with &#60;algorithm&#62; was fixed in C++11 like it was for for loops, but I can't find documentation to that effect. Boost's range algorithms library is still useful here:<p><a href="http://www.boost.org/doc/libs/1_52_0/libs/range/doc/html/index.html" rel="nofollow">http://www.boost.org/doc/libs/1_52_0/libs/range/doc/html/ind...</a>
评论 #4930488 未加载
wonderzombieover 12 years ago
I was a little bit afraid to read this one. I've glanced at some of the functional patterns in the STL — function objects, ugh — and come away shaking my head.<p>But despite my problems with C++ and/or the STL, I really appreciated this piece. The author seems to know his stuff pretty well, as there are references to Haskell, monoids, laziness, as well as a brief discussion of performance.<p>It's probably too much to hope that this will win terribly many converts, but hey, what do I know?
评论 #4929725 未加载
alexchamberlainover 12 years ago
I've recently discovered the delights of &#60;algorithm&#62;... now if I could just figure out how to bind member functions in my build environment (Solaris) I'd be a very happy man.
评论 #4930529 未加载