C++进阶:STL算法15--算术算法

1. 简介

在头文件中。

函数 作用 文档
accumulate(beg,end,val) 对[beg,end)内元素之和,加到初始值val上。 accumulate()
accumulate(beg,end,val,binary) 将函数binary代替加法运算,执行accumulate() accumulate()
partial_sum(beg,end,res) 将[beg,end)内该位置前所有元素之和放进res中。 partial_sum()
partial_sum(beg,end,res,binary) 将函数binary代替加法运算,执行partial_sum() partial_sum()
adjacent_difference(beg1,end1,res) 将[beg,end)内每个新值代表当前元素与上一个元素的差放进res中。 adjacent_difference()
adjacent_difference(beg1,end1,res,binary) 将函数binary代替减法运算,执行adjacent_difference() adjacent_difference()
inner_product(beg1,end1,beg2,val) 对两个序列做内积(对应元素相乘,再求和)并将内积加到初始值val上。 inner_product()
inner_product(beg1,end1,beg2,val,binary1,binary2) 将函数binary1代替加法运算,将binary2代替乘法运算,执行inner_product() inner_product()

2. 示例代码

  • accumulate
// accumulate example
#include      // std::cout
#include    // std::minus
#include       // std::accumulate

int myfunction (int x, int y) {return x+2*y;}
struct myclass {
    int operator()(int x, int y) {return x+3*y;}
} myobject;

int main () {
  int init = 100;
  int numbers[] = {10,20,30};

  std::cout << "using default accumulate: ";
  std::cout << std::accumulate(numbers,numbers+3,init);
  std::cout << '\n';

  std::cout << "using functional's minus: ";
  std::cout << std::accumulate (numbers, numbers+3, init, std::minus());
  std::cout << '\n';

  std::cout << "using custom function: ";
  std::cout << std::accumulate (numbers, numbers+3, init, myfunction);
  std::cout << '\n';

  std::cout << "using custom object: ";
  std::cout << std::accumulate (numbers, numbers+3, init, myobject);
  std::cout << '\n';

  return 0;
}
  • partial_sum
// partial_sum example
#include      // std::cout
#include    // std::multiplies
#include       // std::partial_sum

int myop (int x, int y) {return x+y+1;}

int main () {
  int val[] = {1,2,3,4,5};
  int result[5];

  std::partial_sum (val, val+5, result);
  std::cout << "using default partial_sum: ";
  for (int i=0; i<5; i++) std::cout << result[i] << ' ';
  std::cout << '\n';

  std::partial_sum (val, val+5, result, std::multiplies());
  std::cout << "using functional operation multiplies: ";
  for (int i=0; i<5; i++) std::cout << result[i] << ' ';
  std::cout << '\n';

  std::partial_sum (val, val+5, result, myop);
  std::cout << "using custom function: ";
  for (int i=0; i<5; i++) std::cout << result[i] << ' ';
  std::cout << '\n';
  return 0;
}
  • adjacent_difference
// adjacent_difference example
#include      // std::cout
#include    // std::multiplies
#include       // std::adjacent_difference

int myop (int x, int y) {return x+y;}

int main () {
  int val[] = {1,2,3,5,9,11,12};
  int result[7];

  std::adjacent_difference (val, val+7, result);
  std::cout << "using default adjacent_difference: ";
  for (int i=0; i<7; i++) std::cout << result[i] << ' ';
  std::cout << '\n';

  std::adjacent_difference (val, val+7, result, std::multiplies());
  std::cout << "using functional operation multiplies: ";
  for (int i=0; i<7; i++) std::cout << result[i] << ' ';
  std::cout << '\n';

  std::adjacent_difference (val, val+7, result, myop);
  std::cout << "using custom function: ";
  for (int i=0; i<7; i++) std::cout << result[i] << ' ';
  std::cout << '\n';
  return 0;
}
  • inner_product
// inner_product example
#include      // std::cout
#include    // std::minus, std::divides
#include       // std::inner_product

int myaccumulator (int x, int y) {return x-y;}
int myproduct (int x, int y) {return x+y;}

int main () {
  int init = 100;
  int series1[] = {10,20,30};
  int series2[] = {1,2,3};

  std::cout << "using default inner_product: ";
  std::cout << std::inner_product(series1,series1+3,series2,init);
  std::cout << '\n';

  std::cout << "using functional operations: ";
  std::cout << std::inner_product(series1,series1+3,series2,init,
                                  std::minus(),std::divides());
  std::cout << '\n';

  std::cout << "using custom functions: ";
  std::cout << std::inner_product(series1,series1+3,series2,init,
                                  myaccumulator,myproduct);
  std::cout << '\n';

  return 0;
}

3. 练习

你可能感兴趣的:(C++进阶:STL算法15--算术算法)