accumulate定义于头文件 <numeric>,计算给定值初始值与给定范围 [first, last) 中元素的和。
函数原型
template< class InputIt, class T >
constexpr T accumulate( InputIt first, InputIt last, T init );
template< class InputIt, class T, class BinaryOperation >
constexpr T accumulate( InputIt first, InputIt last, T init,
BinaryOperation op );
参数
- first, last - 要求和的元素范围
- init - 和的初值
- op - 被使用的二元函数对象。接收当前积累值 a (初始化为 init )和当前元素 b 的二元运算符。
该函数的签名应当等价于:Ret fun(const Type1 &a, const Type2 &b);
签名中并不需要有 const &。
类型 Type1 必须使得 T 类型的对象能隐式转换到 Type1 。类型 Type2 必须使得 InputIt 类型的对象能在解引用后隐式转换到 Type2 。 类型 Ret 必须使得 T 类型对象能被赋 Ret 类型值。 - 返回值
- 给定值与给定范围中的元素的和。
- 给定范围在 op 上左折叠的结果
注:
op
操作必须不非法化涉及范围的任何迭代器,含尾迭代器,且不修改其所涉及范围的任何元素及 *last 。- std::accumulate 进行左折叠。要进行右折叠,必须逆转二元运算符的参数顺序,并使用逆序迭代器,即
std::accumulate(rbegin, rend, initval, func)
。
可能的实现方式:
template<class InputIt, class T>
constexpr // C++20 起
T accumulate(InputIt first, InputIt last, T init)
{
for (; first != last; ++first) {
init = std::move(init) + *first; // C++20 起有 std::move
}
return init;
}
template<class InputIt, class T, class BinaryOperation>
constexpr // C++20 起
T accumulate(InputIt first, InputIt last, T init,
BinaryOperation op)
{
for (; first != last; ++first) {
init = op(std::move(init), *first); // C++20 起有 std::move
}
return init;
}
测试用例
注意,以下测试用例分别用到了这几种函数:
- std::iota:可用于按顺序递增填充[first, last)范围的值。即我们传入初始值,而后自增的赋值到指点区间内。
- std::multiplies:是二进制函数对象类,其调用返回其两个参数乘法的结果。此类的对象可用于标准算法,如 transform、accumulate
- std::move:将左值变为右值。在这里的作用是将a变为右值,避免因调用拷贝构造而申请新的内存空间,因此这里调用了移动构造函数(注:移动构造函数通过右值引用的方式传参,构造的新对象的数据成员不生成新的空间,而是共享了传入参数所在的数据成员的内存空间)。
- std::next:用来获取一个距离指定迭代器 n 个元素的迭代器。与之相似的还有std::prev 用来获取一个距离指定迭代器 n 个元素的迭代器。以及std::advance 将将指定迭代器前移或后移 n 个位置的距离。
下面是测试代码:
int main()
{
std::vector<int> vec(10);
std::iota(vec.begin(), vec.end(), 1);// { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int sum = std::accumulate(vec.begin(), vec.end(), 0); // 累加,初值为0
// 计算阶乘 multiplies是二进制函数对象,其调用返回其两个参数乘法的结果
int product = std::accumulate(vec.begin(), vec.end(), 1, std::multiplies<int>());
// 使用匿名函数构建可调用对象,参与到accumulate的op运算中。
// 此可调用对象功能为返回 a,b ,‘- ’的拼接结果
auto dash_fold = [](std::string a, int b) {
return std::move(a) + '-' + std::to_string(b);
};
std::string s = std::accumulate(std::next(vec.begin()), vec.end(),
std::to_string(vec[0]), // 用首元素开始
dash_fold);
// 使用逆向迭代器右折叠
std::string rs = std::accumulate(std::next(vec.rbegin()), vec.rend(),
std::to_string(vec.back()), // 用首元素开始
dash_fold);
std::cout << "sum: " << sum << '\n'
<< "product: " << product << '\n'
<< "dash-separated string: " << s << '\n'
<< "dash-separated string (right-folded): " << rs << '\n';
}
/* 输出结果:
sum: 55
product: 3628800
dash-separated string: 1-2-3-4-5-6-7-8-9-10
dash-separated string (right-folded): 10-9-8-7-6-5-4-3-2-1
*/
补充几种常见的op操作:
- std::multiplies:其调用返回其两个参数乘法的结果。
- std::minus:其调用返回从第一个参数中减去其第二个参数的结果。
- std::plus:其调用返回两个参数加上的结果。其中 accumulate 默认此方法计算。
- std::divides:调用返回将第一个参数除以第二个参数的结果
- std::modulus:调用返回其两个参数之间的模态操作的结果。
// std::minus
int numbers[] = { 10,20,30 };
int result = std::accumulate(numbers, numbers + 3, 100, std::minus<int>());
std::cout << "The minus of 100-10-20-30 is " << result << ".\n"; // 40
// std::plus
int num[] = { 10,20,30 };
int res = std::accumulate(num, num + 3, 100, std::plus<int>());
std::cout << "The sum of 100+10+20+30 is " << res << ".\n"; // 160
// std::multiplies
int nums[] = { 10,20,30 };
int ress = std::accumulate(nums, nums + 3, 100, std::multiplies<int>());
std::cout << "The times of 100*10*20*30 is " << ress << ".\n"; // 600000
// std::divides
int array[] = { 1,2,3 };
int ret = std::accumulate(array, array + 3, 101, std::divides<int>());
std::cout << "The Divide of 101/1/2/3 is " << ret << ".\n"; // 16