C++ 标准库 数值算法

目录

        • 一 概述
        • 二 辅助函数
        • 三 std::accumulate
        • 四 std::inner_product
        • 五 std::adjacent_difference
        • 六 std::partial_sum
        • 七 github
        • 八 参考

一 概述

  • 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)
  • ExecutionPolicy 请参考此前文章 std::find std::execution。
  • Demo
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

八 参考

  • 《C++ 标准库 第2版》
  • cppreference

你可能感兴趣的:(#,C++98/03,#,STL,accumulate,inner_product,partial_sum)