C++ 函式庫 - 乘法



描述

它是一個乘法函式物件類和二元函式物件類,其呼叫返回其兩個引數相乘的結果(如運算子 * 返回的那樣)。

宣告

以下是 std::multiplies 的宣告。

template <class T> struct multiplies;

C++11

template <class T> struct multiplies;

引數

T − 它表示函式呼叫引數和返回值的型別。

返回值

異常

noexcep − 它不丟擲任何異常。

示例

以下示例說明了 std::multiplies 的用法。

#include <iostream>
#include <functional>
#include <numeric>

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<5; i++)
      std::cout << "factorial of " << numbers[i] << "! is " << factorials[i] << '\n';
   return 0;
}

讓我們編譯並執行上述程式,這將產生以下結果:

factorial of 1! is 1
factorial of 2! is 2
factorial of 3! is 6
factorial of 4! is 24
factorial of 5! is 120
functional.htm
廣告

© . All rights reserved.