使用对象或函数累积第四个参数

时间:2018-09-14 13:57:26

标签: function parameters accumulate

有人可以在下面的参考文献中进行解释吗:http://www.cplusplus.com/reference/numeric/accumulate/进行积累

当您为累加的最后一个示例甚至自定义函数示例提供“ myobject”时,它如何工作?就像它是如何获得参数中x和y的值的?

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<int>());
  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;
}

0 个答案:

没有答案