std::partial_sum用于求取序列的累加值,例如:原序列为:1, 2, 3, 4, 5,累加后的序列为:1, 3, 6, 10, 15。可以使用带系数的版本,例如乘以系数1.2,累加后的序列为:1, 3.6, 7.92, 14.304, 23.1648。
示例代码如下:
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <numeric>
#include <sstream>
#include <vector>
int main(int, char **) {
std::vector<int> input_data(10, 2);
// Or
// std::vector<int> input_data = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2};
// Example 1
std::cout << "The first 10 even numbers are: ";
std::partial_sum(input_data.begin(), input_data.end(),
std::ostream_iterator<int>(std::cout, " "));
std::cout << "\n";
// Example 2
std::vector<int> ouput_data(10, 0);
std::partial_sum(

本文介绍C++标准库中std::partial_sum函数的使用方法,并通过多个示例展示如何对不同序列进行累加操作。示例包括基本整数累加、幂级数计算、浮点数累加及系数累加等。
最低0.47元/天 解锁文章
73

被折叠的 条评论
为什么被折叠?



