码迷,mamicode.com
首页 > 编程语言 > 详细

泛型算法一

时间:2014-11-28 09:50:38      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:io   ar   os   sp   for   strong   on   art   bs   

1.accumulate:Computes the sum of all the elements in a specified range including some initial value by computing successive partial sums or computes the result of successive partial results similarly obtained from using a specified binary operation other than the sum.

2函数原型(#include <numeric>)

template<class InputIterator, class Type>
   Type accumulate(
      InputIterator _First, 
      InputIterator _Last, 
      Type _Val
   );
template<class InputIterator, class Type, class BinaryOperation>
   Type accumulate(
      InputIterator _First, 
      InputIterator _Last, 
      Type _Val, 
      BinaryOperation _Binary_op
   );
3参数
_First

An input iterator addressing the first element in the range to be summed or combined according to a specified binary operation.

_Last

An input iterator addressing the last element in the range to be summed or combined according to a specified binary operation that is one position beyond the final element actually included in the iterated accumulation.

_Val

An initial value to which each element is in turn added or combined with according to a specified binary operation.

_Binary_op

string sum = accumulate(v.begin(), v.end(), string(""));  v里每个string元素都连起来,注意这里必须是string(""), 不能是""。

4返回值

The sum of _Val and all the elements in the specified range for the first template function, or, for the second template function, the result of applying the binary operation specified, instead of the sum operation, to (PartialResult, *Iter), where PartialResult is the result of previous applications of the operation and Iter is an iterator pointing to an element in the range.

5例子

int sum = accumulate(vec.begin(), vec.end(), 42) sum设置为vec的元素之和再加上42.

string sum = accumulate(v.begin(), v.end(), string(""));  v里每个string元素都连起来,注意这里必须是string(""), 不能是""。

泛型算法一

标签:io   ar   os   sp   for   strong   on   art   bs   

原文地址:http://www.cnblogs.com/Mr-Zhong/p/4127706.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!