共用方式為


Basic Math Functions

說明如何使用在標準樣板程式庫 (STL) 在 Visual C++ 中的一些基本的數學函式。

template<class _TYPE>
   struct plus : binary_function<_TYPE, _TYPE, _TYPE>
   {
      _TYPE operator()(const _TYPE& _X, const _TYPE& _Y) const
      {return (_X + _Y); }
   };
template<class _TYPE>
   struct minus : binary_function<_TYPE, _TYPE, _TYPE>
   {
      _TYPE operator()(const _TYPE& _X, const _TYPE& _Y) const
      {return (_X - _Y); }
   };
template<class _TYPE>
   struct times : binary_function<_TYPE, _TYPE, _TYPE>
   {
      _TYPE operator()(const _TYPE& _X, const _TYPE& _Y) const
     {return (_X * _Y); }
   };
template<class _TYPE>
   struct divides : binary_function<_TYPE, _TYPE, _TYPE>
   {
      _TYPE operator()(const _TYPE& _X, const _TYPE& _Y) const
      {return (_X / _Y); }
   };
template<class _TYPE>
   struct modulus : binary_function<_TYPE, _TYPE, _TYPE>
   {
      _TYPE operator()(const _TYPE& _X, const _TYPE& _Y) const
      {return (_X % _Y); }
   };

備註

注意事項注意事項

在原型中的類別/參數名稱不相符的標頭檔中的版本。某些已修改以提高可讀性。

這個範例會使用衍生自所有的五個基本的數學結構: 再加上,再減去,乘法地界,] 和 [模數,使用整數做為樣板化的運算元。

範例

// mathfunc.cpp
// compile with: /EHsc
// 
// Structures: plus<A>    - Adds data type A object to
//                          a class object derived from plus.
//             minus<A>   - Subtracts data type A.
//             multiplies<A>   - Multiplies object by data type A.
//             divides<A> - Divides object by data type A.
//             modulus<A> - Returns object modulo A.

#include <functional>
#include <iostream>

using namespace std ;

class MathOps : public plus<int>, public minus<int>,
                 public multiplies<int>, public divides<int>,
                 public modulus<int>
{
public:
  int value;
  MathOps(){value=0;}
  MathOps(int x){value=x;}
  result_type operator+(second_argument_type add2)
                            {return value + add2;}
  result_type operator-(second_argument_type sub2)
                            {return value - sub2;}
  result_type operator*(second_argument_type mult2)
                            {return value * mult2;}
  result_type operator/(second_argument_type div2)
                            {return value / div2;}
  result_type operator%(second_argument_type mod2)
                            {return value % mod2;}
  friend ostream& operator<<(ostream& os, const MathOps& obj ) ;
};

ostream& operator<<(ostream& os, const MathOps& obj )
{
       os << obj.value ;
       return os ;
}

int main(void)
{
  MathOps one,two,three,four,five,six;

  cout << "Using MathOps class..." << endl ;

  one = 18;
  cout << "one = " << one << endl ;

  two = one + 1;
  cout << "two = one + 1 = " << two << endl ;

  three = two - 2;
  cout << "three = two - 2 = " << three << endl ;

  four = three * 3;
  cout << "four = three * 3 = " << four << endl ;

  five = four / 4;
  cout << "five = four / 4 = " << five << endl ;

  six = five % 5;
  cout << "six = five % 5 = " << six << endl ;
}

Output

Using MathOps class...
one = 18
two = one + 1 = 19
three = two - 2 = 17
four = three * 3 = 51
five = four / 4 = 12
six = five % 5 = 2

需求

標頭: <functional>

請參閱

參考

<functional>

概念

標準樣板程式庫範例

其他資源

<functional> 成員

<valarray> 成員