C++ numeric库简介与使用指南

 更新时间:2025年09月03日 10:34:13   作者:Tipriest_  
<numeric> 是 C++标准库中的一个头文件,主要提供了一些数值运算工具函数,用来对序列(通常是容器中的元素)进行累加、相邻差值计算、内积、序列生成等操作,本文给大家介绍C++ numeric库简介与使用指南,感兴趣的朋友跟随小编一起看看吧

好的,我会帮你详细介绍 C++ <numeric> 及其常用函数,并附带示例代码,方便理解。
以下内容会涵盖 <numeric> 的主要功能、常用函数、适用场景以及注意事项。

1.<numeric>库简介

<numeric> 是 C++ 标准库中的一个头文件,主要提供了一些 数值运算工具函数,用来对序列(通常是容器中的元素)进行累加、相邻差值计算、内积、序列生成等操作。

常用特性:

  • 工作对象:任何支持迭代器(尤其是输入迭代器)的序列,例如 std::vectorstd::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.1std::accumulate

功能: 对一个迭代器范围内的元素求和,或用自定义函数进行累积。

语法:

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);

示例:

#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.2std::inner_product

功能: 计算两个序列的内积,可以自定义加法和乘法运算。

语法:

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);

示例:

#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.3std::adjacent_difference

功能: 生成一个新序列,每个元素为输入序列中相邻两个元素的差值(可自定义二元运算)。

语法:

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);

示例:

#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.4std::partial_sum

功能: 生成部分和序列(可自定义二元运算)。

语法:

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);

示例:

#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.5std::iota

功能: 填充一个范围,从某个初始值开始递增。

语法:

template<class ForwardIt, class T>
void iota(ForwardIt first, ForwardIt last, T value);

示例:

#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. 使用建议与注意事项

  1. 当使用浮点数时,累加运算可能会有 精度误差(特别是大规模数据)。
  2. 对于需要高性能的并行数值计算,可以使用 std::reducestd::transform_reduce(C++17+)。
  3. 这些算法可以搭配 标准函数对象(如 std::plus<>std::multiplies<>)或 lambda函数,灵活性很高。
  4. <numeric> 函数不会修改原序列,除非明确要求将结果输出到同一位置(如 partial_sum)。

到此这篇关于C++ numeric库简介与使用指南的文章就介绍到这了,更多相关C++ numeric库使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C/C++实现快速排序(两种方式)图文详解

    C/C++实现快速排序(两种方式)图文详解

    这篇文章主要介绍了C/C++实现快速排序的方法,这几天在找工作,被问到快速排序,结果想不出来快速排序怎么弄的;回来搜索了一下,现在记录下来,方便以后查看
    2021-08-08
  • C语言模拟实现通讯录程序过程

    C语言模拟实现通讯录程序过程

    这篇文章主要介绍了C语言模拟实现通讯录程序过程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2023-02-02
  • 详解C++ bitset用法

    详解C++ bitset用法

    这篇文章主要介绍了C++ bitset用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-03-03
  • C语言实现字符串相连的四种方法

    C语言实现字符串相连的四种方法

    本文介绍了C语言中四种常见的字符串连接方法:strcat函数直接连接,sprintf函数格式化输出,先复制后连接(strcpy+strcat),以及使用指针操作,详细展示了每个方法的代码示例,需要的朋友可以参考下
    2025-07-07
  • c++ 类和对象总结

    c++ 类和对象总结

    这篇文章主要介绍了c++ 类和对象总结一,需要的朋友可以参考下
    2020-01-01
  • OpenMP中For Construct对dynamic的调度方式详解

    OpenMP中For Construct对dynamic的调度方式详解

    在本篇文章当中主要给大家介绍 OpenMp for construct 的实现原理,与他相关的动态库函数分析以及对 dynamic 的调度方式进行分析,希望对大家有所帮助
    2023-02-02
  • VC++ 6.0 C语言实现俄罗斯方块详细教程

    VC++ 6.0 C语言实现俄罗斯方块详细教程

    这篇文章主要为大家介绍了VC++ 6.0 C语言实现俄罗斯方块详细教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-06-06
  • opencv3/C++图像边缘提取方式

    opencv3/C++图像边缘提取方式

    今天小编就为大家分享一篇opencv3/C++图像边缘提取方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-12-12
  • OpenGL实现不规则区域填充算法

    OpenGL实现不规则区域填充算法

    这篇文章主要为大家详细介绍了OpenGL实现不规则区域填充算法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-02-02
  • C++ 系统String类详解

    C++ 系统String类详解

    这篇文章主要介绍了C++的系统String类,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-11-11

最新评论