跳转到内容
主菜单
主菜单
移至侧栏
隐藏
导航
首页
最近更改
随机页面
MediaWiki帮助
代码酷
搜索
搜索
中文(中国大陆)
外观
创建账号
登录
个人工具
创建账号
登录
未登录编辑者的页面
了解详情
贡献
讨论
编辑“︁
C++ 数值操作
”︁
页面
讨论
大陆简体
阅读
编辑
编辑源代码
查看历史
工具
工具
移至侧栏
隐藏
操作
阅读
编辑
编辑源代码
查看历史
常规
链入页面
相关更改
特殊页面
页面信息
外观
移至侧栏
隐藏
您的更改会在有权核准的用户核准后向读者展示。
警告:
您没有登录。如果您进行任何编辑,您的IP地址会公开展示。如果您
登录
或
创建账号
,您的编辑会以您的用户名署名,此外还有其他益处。
反垃圾检查。
不要
加入这个!
= C++数值操作 = == 介绍 == '''C++数值操作'''是C++标准模板库(STL)算法中专门用于处理数值计算的一组函数,定义在<<code><numeric></code>>头文件中。这些函数提供高效且类型安全的数值运算方式,包括累加、内积、部分和、相邻差等操作,适用于数组、向量等容器的数值处理。 数值操作算法是泛型编程的典型应用,通过迭代器抽象与容器解耦,支持自定义数值类型和运算规则。对于初学者,掌握这些函数能显著简化数值计算代码;对于高级用户,可通过自定义函数对象实现复杂数值逻辑。 == 核心算法 == === accumulate === 计算区间内元素的累加和或自定义二元操作的累积结果。 '''语法:''' <syntaxhighlight lang="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 ); </syntaxhighlight> '''示例:''' <syntaxhighlight lang="cpp"> #include <numeric> #include <vector> #include <iostream> 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"; // 输出: Sum: 15 // 自定义操作(乘积) int product = std::accumulate(v.begin(), v.end(), 1, std::multiplies<int>()); std::cout << "Product: " << product; // 输出: Product: 120 } </syntaxhighlight> === inner_product === 计算两个区间的内积(点积)或自定义二元操作的类似计算。 '''语法:''' <syntaxhighlight lang="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 ); </syntaxhighlight> '''示例(向量点积):''' <syntaxhighlight lang="cpp"> std::vector<double> a{1.0, 2.0, 3.0}; std::vector<double> b{4.0, 5.0, 6.0}; double dot = std::inner_product(a.begin(), a.end(), b.begin(), 0.0); // 1*4 + 2*5 + 3*6 = 32 </syntaxhighlight> === partial_sum === 计算区间元素的部分和序列或自定义二元操作的部分结果。 '''语法:''' <syntaxhighlight lang="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 ); </syntaxhighlight> '''示例:''' <syntaxhighlight lang="cpp"> std::vector<int> data{2, 3, 5, 7}, result(4); std::partial_sum(data.begin(), data.end(), result.begin()); // result = {2, 5, 10, 17} </syntaxhighlight> === adjacent_difference === 计算相邻元素的差值或自定义二元操作的结果。 '''语法:''' <syntaxhighlight lang="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 ); </syntaxhighlight> '''示例(计算差分):''' <syntaxhighlight lang="cpp"> std::vector<int> v{4, 6, 9, 13}, diff(4); std::adjacent_difference(v.begin(), v.end(), diff.begin()); // diff = {4, 2, 3, 4} (第一个元素保持原值) </syntaxhighlight> == 数学公式支持 == 部分数值算法对应数学概念: * 累加:<math>\text{accumulate} \rightarrow S = \sum_{i=first}^{last} x_i</math> * 内积:<math>\text{inner\_product} \rightarrow P = \sum_{i} (a_i \times b_i)</math> == 性能特征 == <mermaid> flowchart LR A[算法] --> B[时间复杂度] B -->|accumulate| C[O(n)] B -->|inner_product| D[O(n)] B -->|partial_sum| E[O(n)] B -->|adjacent_difference| F[O(n)] </mermaid> 所有数值操作算法都具有线性时间复杂度,且不分配额外内存(输出迭代器除外)。 == 实际应用案例 == '''案例1:金融计算 - 复利累积''' <syntaxhighlight lang="cpp"> std::vector<double> rates{0.01, 0.02, 0.015}; // 各期利率 double principal = 1000.0; auto compound = [](double acc, double rate) { return acc * (1 + rate); }; double total = std::accumulate(rates.begin(), rates.end(), principal, compound); // 计算: 1000*(1.01)*(1.02)*(1.015) </syntaxhighlight> '''案例2:信号处理 - 移动平均''' <syntaxhighlight lang="cpp"> std::vector<int> signal{3, 5, 7, 2, 8}, ma_result(5); std::partial_sum(signal.begin(), signal.end(), ma_result.begin()); // 转换为移动平均需后续处理:ma_result[i] /= (i+1) </syntaxhighlight> == 高级用法 == 通过自定义函数对象实现复杂数值操作: <syntaxhighlight lang="cpp"> // 计算加权标准差 struct WeightedSum { double operator()(double acc, std::pair<double, double> p) const { return acc + p.first * p.second; // weight * value } }; std::vector<std::pair<double, double>> weighted_data{{0.5, 10}, {1.5, 20}}; double weighted_total = std::accumulate(weighted_data.begin(), weighted_data.end(), 0.0, WeightedSum()); </syntaxhighlight> == 最佳实践 == 1. 对浮点数使用<code>std::accumulate</code>时,初始化值应显式指定类型(如<code>0.0</code>而非<code>0</code>) 2. 自定义操作函数应满足结合律(对于并行计算很重要) 3. 大数计算时考虑使用<code>std::multiplies<void></code>等透明运算符避免类型转换 == 参见 == * [[C++算法库]] * [[泛型编程]] * [[迭代器模式]] [[Category:编程语言]] [[Category:C++]] [[Category:C++stl 算法]]
摘要:
请注意,所有对代码酷的贡献均被视为依照知识共享署名-非商业性使用-相同方式共享发表(详情请见
代码酷:著作权
)。如果您不希望您的文字作品被随意编辑和分发传播,请不要在此提交。
您同时也向我们承诺,您提交的内容为您自己所创作,或是复制自公共领域或类似自由来源。
未经许可,请勿提交受著作权保护的作品!
取消
编辑帮助
(在新窗口中打开)