How do you use stl functions like for_each?

综合编程 2018-01-12 阅读原文

I started using stl containers because they came in very handy when I needed the functionality of a list, set and map and had nothing else available in my programming environment. I did not care much about the ideas behind it. STL documentation was interesting up to the point where it came to functions, etc. Then I skipped reading and just used the containers.

But yesterday, still being relaxed from my holidays, I just gave it a try and wanted to go a bit more the stl way. So I used the transform function (can I have a little bit of applause for me, thank you).

From an academic point of view it really looked interesting and it worked. But the thing that bothers me is that if you intensify the use of those functions, you need thousands of helper classes for mostly everything you want to do in your code. The whole logic of the program is sliced into tiny pieces. This slicing is not the result of good coding habits; it's just a technical need. Something, that makes my life probably harder not easier.

I learned the hard way, that you should always choose the simplest approach that solves the problem at hand. I can't see what, for example, the for_each function is doing for me that justifies the use of a helper class over several simple lines of code that sit inside a normal loop so that everybody can see what is going on.

I would like to know, what you are thinking about my concerns? Did you see it like I do when you started working this way and have changed your mind when you got used to it? Are there benefits that I overlooked? Or do you just ignore this stuff as I did (and will go on doing it, probably).

Thanks.

PS: I know that there is a real for_each loop in boost. But I ignore it here since it is just a convenient way for my usual loops with iterators I guess.

The whole logic of the program is sliced in tiny pieces. This slicing is not the result of good coding habits. It's just a technical need. Something, that makes my life probably harder not easier.

You're right, to a certain extent. That's why the upcoming revision to the C++ standard will add lambda expressions, allowing you to do something like this:

std::for_each(vec.begin(), vec.end(), [&](int& val){val++;})

but I also think it is often a good coding habit to split up your code as currently required. You're effectively separating the code describing the operation you want to do, from the act of applying it to a sequence of values. It is some extra boilerplate code, and sometimes it's just annoying, but I think it also often leads to good, clean, code.

Doing the above today would look like this:

int incr(int& val) { return val+1}

// and at the call-site
std::for_each(vec.begin(), vec.end(), incr);

Instead of bloating up the call site with a complete loop, we have a single line describing:

  • which operation is performed (if it is named appropriately)
  • which elements are affected

so it's shorter, and conveys the same information as the loop, but more concisely. I think those are good things. The drawback is that we have to define the incr
function elsewhere. And sometimes that's just not worth the effort, which is why lambdas are being added to the language.

Hello, buddy!

责编内容by:Hello, buddy!阅读原文】。感谢您的支持!

您可能感兴趣的

anomalize: Tidy Anomaly Detection We recently had an awesome opportunity to work with a great client that asked Business Science to build an open source ...
STL整理之set 转载请注明出处,部分内容引自李煜东《算法竞赛进阶指南》 前置知识: C++、C语言入门 Set是什么 ...
Using STL algorithms with cppcheck Raw loops From Sean Parent’s C++ Seasoning talk, he explains how raw loops suffer from several issues: Di...
SGISTL源码探究-STL中的红黑树(下) 前言 在上一小节中,我们针对SGISTL中的红黑树的分析完成了一半,接下来的各种操作才是重点的部分。通过以下分析可以了解到红黑树是如何保持平衡的。(STL中实现红黑树定义的重载版本函数太多,我舍去了一部分定义,直接对其声明进行分析...
之stcak的用法 stack #include , 堆栈 这个就是咱们数据结构中学的栈,栈的操作只有几种方法 声明 stack s; 入栈 s.push(8); 出栈 ...