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语言对栈的实现基本操作
    2021-07-07
  • C++ OpenCV实现图像双三次插值算法详解

    C++ OpenCV实现图像双三次插值算法详解

    图像双三次插值的原理,就是目标图像的每一个像素都是由原图上相对应点周围的4x4=16个像素经过加权之后再相加得到的。本文主要介绍了通过C++ OpenCV实现图像双三次插值算法,需要的可以参考一下
    2021-12-12
  • C语言二叉树常见操作详解【前序,中序,后序,层次遍历及非递归查找,统计个数,比较,求深度】

    C语言二叉树常见操作详解【前序,中序,后序,层次遍历及非递归查找,统计个数,比较,求深度】

    这篇文章主要介绍了C语言二叉树常见操作,结合实例形式详细分析了基于C语言的二叉树前序,中序,后序,层次遍历及非递归查找,统计个数,比较,求深度等相关操作技巧与注意事项,需要的朋友可以参考下
    2018-04-04
  • C++中this指针用法详解及实例

    C++中this指针用法详解及实例

    这篇文章主要介绍了C++中this指针用法详解及实例的相关资料,需要的朋友可以参考下
    2017-04-04
  • C++标准库(std)用法解读

    C++标准库(std)用法解读

    本文介绍了C++标准库的主要组成部分及其使用方法,涵盖命名空间、输入输出流、字符串处理、STL容器、算法、数值处理、函数对象与可调用对象、异常处理、时间日期、文件系统、线程支持和正则表达式等,示例代码展示了如何使用这些功能
    2026-04-04
  • C/C++中数据类型转换详解及其作用介绍

    C/C++中数据类型转换详解及其作用介绍

    这篇文章主要介绍了C/C++中数据类型转换详解及其作用,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-09-09
  • C++实现LeetCode(137.单独的数字之二)

    C++实现LeetCode(137.单独的数字之二)

    这篇文章主要介绍了C++实现LeetCode(137.单独的数字之二),本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-07-07
  • C语言编程中对目录进行基本的打开关闭和读取操作详解

    C语言编程中对目录进行基本的打开关闭和读取操作详解

    这篇文章主要介绍了C语言编程中对目录进行基本的打开关闭和读取操作,是C语言入门学习中的基础知识,需要的朋友可以参考下
    2015-09-09
  • QT中start()和startTimer()的区别小结

    QT中start()和startTimer()的区别小结

    QTimer提供了定时器信号和单触发定时器,本文主要介绍了QT中start()和startTimer()的区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-09-09
  • vscode终端中打不开conda虚拟包管理的解决

    vscode终端中打不开conda虚拟包管理的解决

    本文主要介绍了vscode终端中打不开conda虚拟包管理的解决,文中通过图文介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-09-09

最新评论