二進製函數對象類,其調用返回從其第一個參數減去第二個參數的結果(由二進製運算符-返回)。
用法:
template struct minus:binary_function { T operator() (const T& x, const T& y) const {return x-y;} }; 模板參數: T - Type of the arguments and return type of the functional call. The type shall support the operation (binary operator-). 成員類型: x:Type of the first argument in member operator() y:Type of the second argument in member operator() result_type:Type returned by member operator()
// C++ program to illustrate std::minus
// by subtracting all array elements from a number
#include <bits/stdc++.h>
int main()
{
// Array with elements to be subtracted
int arr[] = { 10, 20, 30 };
// size of array
int size = sizeof(arr) / sizeof(arr[0]);
// Variable from which array is to be subtracted
int num = 100;
// Variable to store result
int result;
// using std::accumulate to perform subtraction on array from num
// using std::minus
result = std::accumulate(arr, arr + size, num, std::minus<int>());
// Printing the result
std::cout << "The result of 100-10-20-30 is " << result;
return 0;
}
輸出:
The result of 100-10-20-30 is 40
另一個例子:
// C++ program to illustrate std::minus
// by subtracting the respective elements of 2 arrays
#include <iostream> // std::cout
#include <functional> // std::plus
#include <algorithm> // std::transform
int main()
{
// First array
int first[] = { 100, 200, 300, 400, 500 };
// Second array
int second[] = { 10, 20, 30, 40, 50 };
// Result array
int results[5];
// std::transform applies std::minus to the whole array
std::transform(first, first + 5, second, results, std::minus<int>());
// Printing the result array
for (int i = 0; i < 5; i++)
std::cout << results[i] << " ";
return 0;
}
輸出:
90 180 270 360 450
相關用法
注:本文由純淨天空篩選整理自 std::minus in C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。