Effectiv STL 37 Use accumulate or for_each to summarize ranges
2017-09-21 11:41
393 查看
<numeric>
accumulate, inner_product, adjacent_difference, and partial_sum
Calculate the product of a range of numbers
point average(其实没看懂书上讲的side effect)
accumulate returns the summary ;
for_each returns a function object;
accumulate, inner_product, adjacent_difference, and partial_sum
string::size_type stringLengthSum(string::size_type sumSoFar, const string& s) { return sumSoFar + s.size(); } set<string> ss; string::size_type lengthSum = accumuolate(ss.begin(), ss.end(), static_cast<string::size_type>(0), stringLengthSum);
Calculate the product of a range of numbers
vector<float> vf; ... float product = accumulate(vf.begin(), vf.end(), 1.0f, multiplies<float>());
point average(其实没看懂书上讲的side effect)
struct Point { Point(double initX, double initY): x(initX), y(initY) { } double x, y; }; list<Point> lp; ... class PointAverage: public binary_function<Point, Point, Point> { public: PointAverage():numPoint(0), xSum(0), ySum(0) { } const Point operator()(const Point& avgSoFar, const Point& p) { ++ numPoints; xSum += p.x; ySum += p.y; return Point(xSum / numPoints, ySum / numPoints); } private: size_t numPoints; double xSum; double ySum; }; Point avg = accumulate(lp.begin(), lp.end(), Point(0, 0), PointAverage());
class PointAverage: public unary_function<Point, void> { public: PointAverage():numPoint(0), xSum(0), ySum(0) {} void operator()(const Pint& p) { ++ numPoints; xSum += p.x; ySum += p.y; } Point result() const { return Point(xSum / numPoints, ySum / numPoints); } private: size_t numPoints; double xSum; double ySum; }; list<Point> lp; ... Point avg = for_each(lp.each(lp.begin(), lp.end(), PointAverage()).result();
accumulate returns the summary ;
for_each returns a function object;
相关文章推荐
- 错误 1 error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use . See online help for details.
- How to use jquery or ajax to update razor partial view in c#/asp.net for a MVC project
- which is best to use ? List or Dictionary ? For efficiency of code execution.
- Name for parameter binding must not be null or empty! On JDKs < 8, you need to use @Param for named
- How To Use WordBasic Functions in an MFC Automation Client for Word 97, Word 2000, Word 2002, or Word 2003
- [quote]HowTo Format Date For Display or Use In a Shell Script
- 如何使用th:each属性迭代模板-原标题:How To Use th:each For Iteration In Thymeleaf Template?
- STL的适配器、仿函数学习之一:accumulate和for_each的使用心得
- How to specify image folder path for each user when use TinyMce ImageManager with Symfony
- How to format date for display or to use in a shell script
- STL_Algorithm5-math: random_shuffle, count, count_if, min_element, max_element, accumulate, for_each, transform
- : error C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
- use webdevelop tools to delete domain specific cookies for ecommerical cps union effect
- Use P/Invoke to Develop a .NET Base Class Library for Serial Device Communications
- Define Number Ranges for Each Material Type 关联物料类别和序列号
- Use exceptions to watch for and handle I/O errors.
- STL学习之十四:常用算法 for_each 和 transform以及adjacent_find的示例代码
- stl for_each使用
- Error This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. T
- java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this a