Home C&C++函数库 c++ 语法 程序源码 Linux C库

std::

multiplies

类模板  <functional>

template <class T> struct multiplies;

乘法函数对象类
二元函数对象类,其调用返回两个实参相乘的结果(与操作符*返回的结果相同)。

一般来说,函数对象是定义了成员函数operator()的类的实例。该成员函数允许使用与函数调用相同的语法来使用对象。

它的行为定义如下:

C++98
template <class T> struct multiplies : binary_function <T,T,T> {
  T operator() (const T& x, const T& y) const {return x*y;}
};


该类的对象可用于标准算法,如transform 和 accumulate。

☲  模板参数


T
参数的类型和函数调用的返回类型。
类型应支持操作(operator*)。

☲  成员类型


成员类型 定义 注释
first_argument_type T 成员operator()中第一个参数的类型
second_argument_type T 成员operator()中第二个参数的类型
result_type T 成员operator()返回的类型

☲  成员函数



T operator() (const T& x, const T& y)
返回两个参数(x*y)乘积的成员函数。

☣  示例



// factorials (multiplies example)
#include <iostream>     // std::cout
#include <functional>   // std::multiplies
#include <numeric>      // std::partial_sum

int main () {
  int numbers[9];
  int factorials[9];
  for (int i=0;i<9;i++) numbers[i]=i+1;
  std::partial_sum (numbers, numbers+9, factorials, std::multiplies<int>());
  for (int i=0; i<9; i++)
    std::cout << numbers[i] << "! is " << factorials[i] << '\n';
  return 0;
}

输出:
1! is 1
2! is 2
3! is 6
4! is 24
5! is 120
6! is 720
7! is 5040
8! is 40320
9! is 362880

🍄  另请参阅



plus 加法函数对象类(类模板)
minus 减法函数对象类(类模板)
divides 除法函数对象类(类模板)
modulus 模函数对象类(类模板)
negate 取反函数对象类(类模板)
equal_to 用于相等比较的函数对象类(类模板)

联系我们 免责声明 关于CandCplus 网站地图