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(input_data.begin(), input_data.end(), ouput_data.begin(),
std::multiplies<int>());
std::cout << "The first 10 powers of 2 are: ";
std::copy(ouput_data.begin(), ouput_data.end(),
std::ostream_iterator<int>(std::cout, " "));
// Or
// for (const auto& number: ouput_data) {
// std::cout << number << " ";
// }
std::cout << "\n";
// Example 3
std::istringstream str("0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0");
std::cout << "The partial sum results are: ";
std::partial_sum(std::istream_iterator<double>(str),
std::istream_iterator<double>(),
std::ostream_iterator<double>(std::cout, " "));
// Now str is empty. Don't use it again.
std::cout << "\nHas the end of the string been reached: " << std::boolalpha
<< str.eof();
std::cout << "\n";
// Example 4
std::istringstream str1("1 2 3 4 5 6 7 8 9 10");
constexpr double COEFF = 1.6;
std::cout << "The partial sum results with a coefficient " << COEFF
<< " are: ";
std::partial_sum(
std::istream_iterator<double>(str1), std::istream_iterator<double>(),
std::ostream_iterator<double>(std::cout, " "),
[COEFF](double lhs, double rhs) { return COEFF * (lhs + rhs); });
std::cout << "\n";
// Another implementation method
std::vector<double> input_ouput_data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (size_t i = 1; i < input_ouput_data.size(); ++i) {
input_ouput_data[i] =
(input_ouput_data[i - 1] + input_ouput_data[i]) * COEFF;
}
std::cout << "The partial sum results with a coefficient " << COEFF
<< " are (with another implementation method): ";
std::copy(input_ouput_data.begin(), input_ouput_data.end(),
std::ostream_iterator<double>(std::cout, " "));
std::cout << "\n";
// Example 5
std::istringstream str2("1 3 5 7 8 9 10 ");
std::cout << "The first even number is "
<< *std::find_if(std::istream_iterator<int>(str2),
std::istream_iterator<int>(),
[](int i) { return i % 2 == 0; })
<< ". \n";
bool lhs = true;
bool rhs = false;
std::cout << "lhs > rhs ? " << std::boolalpha << (lhs > rhs) << ".\n";
return 0;
}
CMake编译脚本文件CMakeLists.txt
为:
cmake_minimum_required(VERSION 3.0.0)
project(partial_sum VERSION 0.1.0)
include(CTest)
enable_testing()
add_executable(partial_sum main.cpp)
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
运行结果如下:
The first 10 even numbers are: 2 4 6 8 10 12 14 16 18 20
The first 10 powers of 2 are: 2 4 8 16 32 64 128 256 512 1024
The partial sum results are: 0.1 0.3 0.6 1 1.5 2.1 2.8 3.6 4.5 5.5
Has the end of the string been reached: true
The partial sum results with a coefficient 1.2 are: 1 3.6 7.92 14.304 23.1648 34.9978 50.3973 70.0768 94.8921 125.871
The first even number is 8.