C++ 标准库 数值算法

一 概述
  • C++ 标准库中提供了很多算法,定义于头文件 < algorithm >,少量定义于< numeric >。本文主要探究以下用于 数值区间 的算法,它们定义于< numeric >:
    • std::accumulate 对一个范围内的元素求和
    • std::inner_product 计算两个范围的元素的内积
    • std::adjacent_difference 计算范围内各相邻元素之间的差
    • std::partial_sum 计算范围内元素的部分和
二 辅助函数
template <typename C>
void print(const std::string& pre, C container) {
  std::cout << pre;
  std::copy(container.begin(), container.end(),
            std::ostream_iterator<int>(std::cout, " "));
  std::cout << std::endl;
}
三 std::accumulate
  • 定义
template< class InputIt, class T >
T accumulate( InputIt first, InputIt last, T init );(1)(until C++20)
template< class InputIt, class T >
constexpr T accumulate( InputIt first, InputIt last, T init );(1)(since C++20)	
template< class InputIt, class T, class BinaryOperation >
T accumulate( InputIt first, InputIt last, T init,
              BinaryOperation op );(2)(until C++20)
template< class InputIt, class T, class BinaryOperation >
constexpr T accumulate( InputIt first, InputIt last, T init,
                        BinaryOperation op );(2)(since C++20)
  • Demo
if (1) {
  // std::accumulate 对一个范围内的元素求和
  std::vector<int> vc;
  fill(vc, 1, 6);
  print("vc: ", vc);
  std::cout << "accumulate vc: " << std::accumulate(vc.begin(), vc.end(), 0) << std::endl;
}
  • 结果
vc: 1 2 3 4 5 6
accumulate vc: 21 // 1 + 2 + 3 + 4 + 5 + 6 = 21
四 std::inner_product
  • 定义
template< class InputIt1, class InputIt2, class T >
T inner_product( InputIt1 first1, InputIt1 last1,
                 InputIt2 first2, T init );(1)(C++20)
template< class InputIt1, class InputIt2, class T >
constexpr T inner_product( InputIt1 first1, InputIt1 last1,
                           InputIt2 first2, T init );(1)(C++20)
	
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 );(2)(C++20)
template<class InputIt1, class InputIt2, class T,
         class BinaryOperation1, class BinaryOperation2>
constexpr T inner_product( InputIt1 first1, InputIt1 last1,
                           InputIt2 first2, T init,
                           BinaryOperation1 op1,
                           BinaryOperation2 op2 );(2)(C++20)
if (1) {
  // std::inner_product 计算两个范围的元素的内积
  std::vector<int> vc1;
  fill(vc1, 2, 4);
  std::vector<int> vc2;
  fill(vc2, 3, 5);
  print("vc1: ", vc1);
  print("vc2: ", vc2);
  std::cout << "inner_product vc1 and vc2: "
            << std::inner_product(vc1.begin(), vc1.end(), vc2.begin(), 0)
            << std::endl;
}
  • 结果
vc1: 2 3 4
vc2: 3 4 5
inner_product vc1 and vc2: 38 
// 2*3 + 3*4 + 4*5 = 38
五 std::adjacent_difference
  • 定义
template< class InputIt, class OutputIt >
OutputIt adjacent_difference( InputIt first, InputIt last,
                              OutputIt d_first );(1)(C++20)
template< class InputIt, class OutputIt >
constexpr OutputIt adjacent_difference( InputIt first, InputIt last,
                                        OutputIt d_first );(1)(C++20)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >
ForwardIt2 adjacent_difference( ExecutionPolicy&& policy, ForwardIt1 first, 
ForwardIt1 last, ForwardIt2 d_first );(2)(C++17)
template< class InputIt, class OutputIt, class BinaryOperation >
OutputIt adjacent_difference( InputIt first, InputIt last,
                              OutputIt d_first, BinaryOperation op );(3)(C++20)
template< class InputIt, class OutputIt, class BinaryOperation >
constexpr OutputIt adjacent_difference( InputIt first, InputIt last,
                                        OutputIt d_first, BinaryOperation op );(3)(C++20)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryOperation >
ForwardIt2 adjacent_difference( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last,
                                ForwardIt2 d_first, BinaryOperation op );(4)(C++17)
  • Demo
if (1) {
  // std::adjacent_difference 计算范围内各相邻元素之间的差
  std::vector<int> vc{1, 3, 5, 7};
  print("vc: ", vc);
  std::adjacent_difference(vc.begin(), vc.end(), vc.begin());
  std::cout << "adjacent_difference vc: " << std::endl;
  print("vc: ", vc);
}
  • 结果
vc: 1 3 5 7
adjacent_difference vc:
vc: 1 2 2 2 // 1 (3-1) (5-3) (7-5)
六 std::partial_sum
  • 定义
template< class InputIt, class OutputIt >
OutputIt partial_sum( InputIt first, InputIt last, OutputIt d_first );(1)(C++20)
template< class InputIt, class OutputIt >
constexpr OutputIt partial_sum( InputIt first, InputIt last, OutputIt d_first );(1)(C++20)
	
template< class InputIt, class OutputIt, class BinaryOperation >
OutputIt partial_sum( InputIt first, InputIt last, OutputIt d_first,
                      BinaryOperation op );(2)(C++20)
template< class InputIt, class OutputIt, class BinaryOperation >
constexpr OutputIt partial_sum( InputIt first, InputIt last, OutputIt d_first,
                                BinaryOperation op );(2)(C++20)
  • Demo
if (1) {
  // std::partial_sum 计算范围内元素的部分和
  std::vector<int> vc{1, 3, 5, 7};
  print("vc: ", vc);
  std::partial_sum(vc.begin(), vc.end(), vc.begin());
  std::cout << "partial_sum vc: " << std::endl;
  print("vc: ", vc);
}
  • 结果
vc: 1 3 5 7
partial_sum vc:
vc: 1 4 9 16 // 1 (1+3) (1+3+5) (1+3+5+7)
七 github
  • 所有Demo 已上传github cplusplus
  • numeric_algotithm.cpp
八 参考
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值