好的,我会帮你详细介绍 C++ <numeric>
库 及其常用函数,并附带示例代码,方便理解。
以下内容会涵盖 <numeric>
的主要功能、常用函数、适用场景以及注意事项。
1. <numeric>
库简介
<numeric>
是 C++ 标准库中的一个头文件,主要提供了一些 数值运算工具函数,用来对序列(通常是容器中的元素)进行累加、相邻差值计算、内积、序列生成等操作。
常用特性:
- 工作对象:任何支持迭代器(尤其是输入迭代器)的序列,例如
std::vector
、std::array
、原生数组等。 - 泛型:可以作用于任意类型(只要它们支持相应的运算符,比如
+、-、*
等)。
2. 常用函数总览
函数 | 作用 | 头文件 |
---|---|---|
std::accumulate |
对范围内的元素进行累加或自定义的二元操作 | <numeric> |
std::reduce (C++17) |
类似 accumulate ,但可并行实现(需 <execution> 支持) |
<numeric> |
std::inner_product |
计算两个序列的内积(可自定义加法和乘法运算) | <numeric> |
std::adjacent_difference |
计算相邻元素的差值(或自定义二元操作) | <numeric> |
std::partial_sum |
计算部分和(或自定义二元操作) | <numeric> |
std::inclusive_scan / exclusive_scan (C++17) |
类似 partial_sum ,但有更多控制和并行支持 |
<numeric> |
std::iota |
从一个初始值开始,为范围内的元素按递增填充值 | <numeric> |
3. 常用函数详解
3.1 std::accumulate
功能: 对一个迭代器范围内的元素求和,或用自定义函数进行累积。
语法:
cpp
template<class InputIt, class T>
T accumulate(InputIt first, InputIt last, T init);
template<class InputIt, class T, class BinaryOperation>
T accumulate(InputIt first, InputIt last, T init, BinaryOperation op);
示例:
cpp
#include <iostream>
#include <vector>
#include <numeric> // accumulate
int main() {
std::vector<int> v = {1, 2, 3, 4, 5};
int sum = std::accumulate(v.begin(), v.end(), 0);
std::cout << "Sum: " << sum << "\n";
// 计算乘积
int product = std::accumulate(v.begin(), v.end(), 1, std::multiplies<>());
std::cout << "Product: " << product << "\n";
}
输出:
Sum: 15
Product: 120
3.2 std::inner_product
功能: 计算两个序列的内积,可以自定义加法和乘法运算。
语法:
cpp
template<class InputIt1, class InputIt2, class T>
T inner_product(InputIt1 first1, InputIt1 last1,
InputIt2 first2, T init);
template<class InputIt1, class InputIt2, class T,
class BinaryOperation1, class BinaryOperation2>
T inner_product(InputIt1 first1, InputIt1 last1,
InputIt2 first2, T init,
BinaryOperation1 op1, BinaryOperation2 op2);
示例:
cpp
#include <iostream>
#include <vector>
#include <numeric> // inner_product
int main() {
std::vector<int> a = {1, 2, 3};
std::vector<int> b = {4, 5, 6};
int result = std::inner_product(a.begin(), a.end(), b.begin(), 0);
std::cout << "Inner product: " << result << "\n"; // 32
// 使用自定义操作(这里是 a[i]-b[i] 的和)
int diff_sum = std::inner_product(a.begin(), a.end(), b.begin(), 0,
std::plus<>{}, std::minus<>{});
std::cout << "Sum of differences: " << diff_sum << "\n"; // -9
}
3.3 std::adjacent_difference
功能: 生成一个新序列,每个元素为输入序列中相邻两个元素的差值(可自定义二元运算)。
语法:
cpp
template<class InputIt, class OutputIt>
OutputIt adjacent_difference(InputIt first, InputIt last, OutputIt d_first);
template<class InputIt, class OutputIt, class BinaryOperation>
OutputIt adjacent_difference(InputIt first, InputIt last, OutputIt d_first, BinaryOperation op);
示例:
cpp
#include <iostream>
#include <vector>
#include <numeric> // adjacent_difference
int main() {
std::vector<int> v = {1, 3, 6, 10, 15};
std::vector<int> result(v.size());
std::adjacent_difference(v.begin(), v.end(), result.begin());
for (int n : result) std::cout << n << " "; // 1 2 3 4 5
}
3.4 std::partial_sum
功能: 生成部分和序列(可自定义二元运算)。
语法:
cpp
template<class InputIt, class OutputIt>
OutputIt partial_sum(InputIt first, InputIt last, OutputIt d_first);
template<class InputIt, class OutputIt, class BinaryOperation>
OutputIt partial_sum(InputIt first, InputIt last, OutputIt d_first, BinaryOperation op);
示例:
cpp
#include <iostream>
#include <vector>
#include <numeric> // partial_sum
int main() {
std::vector<int> v = {1, 2, 3, 4, 5};
std::vector<int> result(v.size());
std::partial_sum(v.begin(), v.end(), result.begin());
for (int n : result) std::cout << n << " "; // 1 3 6 10 15
}
3.5 std::iota
功能: 填充一个范围,从某个初始值开始递增。
语法:
cpp
template<class ForwardIt, class T>
void iota(ForwardIt first, ForwardIt last, T value);
示例:
cpp
#include <iostream>
#include <vector>
#include <numeric> // iota
int main() {
std::vector<int> v(5);
std::iota(v.begin(), v.end(), 10); // 从 10 开始
for (int n : v) std::cout << n << " "; // 10 11 12 13 14
}
4. 使用建议与注意事项
- 当使用浮点数时,累加运算可能会有 精度误差(特别是大规模数据)。
- 对于需要高性能的并行数值计算,可以使用
std::reduce
或std::transform_reduce
(C++17+)。 - 这些算法可以搭配 标准函数对象 (如
std::plus<>
、std::multiplies<>
)或 lambda函数,灵活性很高。 <numeric>
函数不会修改原序列,除非明确要求将结果输出到同一位置(如partial_sum
)。