![]() |
1
6
表达式
技术上,
|
![]() |
2
1
[Edit] I originally misunderstood the author's original intent. I corrected the mistake.
First of all, regardless of whether the for_each algorithm copies the functor you pass in is irrelevant. What's relevant is that you should be storing the iterator or pointer to the first element rather than the pointee. If you do that and dereference it each time, it should do the trick.
然而,我将不得不放弃我对函数编程的两点看法。通过函数编程,您可以得到非常简洁、精巧的代码。然而,调试也是一个很大的痛苦,它具有分散代码的效果。除非你的代码能从中受益匪浅,否则你可以考虑简单的基于迭代器的循环,因为这会让你的程序员更容易调试和阅读代码。 过去我犯了一个错误,就是函数式编程过于繁琐,这让我的团队讨厌我。我痴迷于用组合谓词逻辑编写非常严格的代码,并构建了大量的函数对象库,这些函数对象可以反复使用,也可以组合使用。实际上,我所做的是花费大量的时间来编写函子,那时我本来可以编写简单的、同样可重用的函数,而不是从简单的基于迭代器的循环(更容易用C++0x的基于循环的范围和BooStYfFux编写)来调用(并且内联同样容易)。我仍然在C++中使用函数式编程,但要节俭。当你经历了很多困难,并且不断积累时间,将两三行代码组合成一行代码时,你真的需要问问自己,并深入思考它是否值得,不仅是对你自己,而且对每一个使用代码的人来说。 |
![]() |
3
0
Yes, it is what you said. Functors are passed by value by default and thus they are copied in code of std::for_each. However you could write your own version of std::for_each explicitly stating that you want to pass functor by reference. |
![]() |
4
0
对。将为每个函数传递一个新的函数副本allong。你正在读的书解释了这一点。 |
![]() |
5
0
I believe the reason why the functor is copied around like this is to make
设想一个引用该函数的实现。如果该函数是一个右值(例如,如果它是从另一个函数返回的),则会中断:
唯一真正的通用解决方案是通过值传递函子。然后用const、非const、rValm和LValm函子进行运算。 |
![]() |
6
0
你的
This is now fixed (nothing else modifies
这就是
|
|
Julia · 矢量中相加为总和S的值的数量 2 年前 |
![]() |
C_Rod · 在模板方法中确定STL容器中项目的数据类型 3 年前 |
![]() |
quantumwell · 将空向量放入std::map() 7 年前 |
![]() |
OutOfBound · 对未初始化内存使用算法的优点 7 年前 |
![]() |
DarthRubik · 在使用列表删除之后,迭代器如何不无效 7 年前 |