当前位置: 首页 > news >正文

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.1 std::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> // accumulateint 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

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

语法:

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_productint 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

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

语法:

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_differenceint 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

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

语法:

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_sumint 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

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

语法:

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

示例:

#include <iostream>
#include <vector>
#include <numeric> // iotaint 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)。
http://www.dtcms.com/a/362616.html

相关文章:

  • 【LeetCode】1792. 最大平均通过率(康复-T1)
  • 校企合作| 长春大学旅游学院副董事长张海涛率队到访卓翼智能,共绘无人机技术赋能“AI+文旅”发展新蓝图
  • DAG与云计算任务调度优化
  • 【android bluetooth 协议分析 21】【ble 介绍 3】【ble acl Supervision Timeout 介绍】
  • 无人机系统理论基础(有课件)
  • 无人机小尺寸RFSOC ZU47DR板卡
  • 无人机传感器技术要点与难点解析
  • 【无人机三维路径规划】基于遗传算法GA结合粒子群算法PSO无人机复杂环境避障三维路径规划(含GA和PSO对比)研究
  • 基于YOLOv4的无人机视觉手势识别系统:从原理到实践
  • C++面试题
  • 股指期货是股市下跌的原罪,还是风险对冲好帮手?
  • 什么是 DNSSEC?
  • 面试tips--MySQLRedis--Redis 有序集合用跳表不用B+树 MySQL用B+树作为存储引擎不用跳表:原因如下
  • 278-基于Django的协同过滤旅游推荐系统
  • 详解Grafana k6 的阈值(Thresholds)
  • os.path:平台独立的文件名管理
  • sql执行过程
  • Tomcat 全面指南:从目录结构到应用部署与高级配置
  • Java-Spring入门指南(一)Spring简介
  • WPF曲线自定义控件 - CurveHelper
  • 大模型是如何“学会”思考的?——从预训练到推理的全过程揭秘
  • 【完整源码+数据集+部署教程】PHC桩实例分割系统源码和数据集:改进yolo11-Faster-EMA
  • 无需服务器,免费、快捷的一键部署前端 vue React代码--PinMe
  • 搭建分布式Hadoop集群[2025] 实战笔记
  • 【golang长途旅行第36站】golang操作Redis
  • 【自记】Python 中 简化装饰器使用的便捷写法语法糖(Syntactic Sugar)示例
  • ARM汇编记忆
  • 【53页PPT】华为制造行业数字化转型工业互联网智能制造解决方案(附下载方式)
  • MySQL事务+MVCC(精简版,包教包废)
  • 2025华为最值得入的耳机,真的赢麻了!