C ++
 Computer >> コンピューター >  >> プログラミング >> C ++

C++STLのモジュラス関数


この記事では、C ++でのモジュラス関数の動作、構文、および例について説明します。

モジュラス関数C++とは何ですか?

C ++のモジュラス関数オブジェクトクラス。これは、ヘッダーファイルで定義されています。モジュラス関数は、2つの引数のモジュラス演算の結果を取得するために使用されるバイナリ関数オブジェクトクラスです。この関数は、演算子‘%‘と同じように機能します。

モジュラス関数の構文

Template struct modulus : binary_function
{
   T operator() (const T& a, const T& b) const {return a%b; }
};

テンプレートパラメータ

この関数は、次のパラメーターを受け入れます-

  • T −これは関数呼び出しに渡される引数のタイプです。

#include <iostream>
#include <algorithm>
#include <functional&g;
using namespace std;
int main(){
   //create an array
   int arr[] = { 10, 20, 35, 45, 50, 61 };
   int rem[6];
   transform(arr, arr + 6, rem,bind2nd(modulus<int>(), 2));
   for (int i = 0; i < 5; i++){
      cout << arr[i] << " is a "<<(rem[i] == 0 ? "even" : "odd")<<"\n";
   }
   return 0;
}

出力

上記のコードを実行すると、次の出力が生成されます-

10 is a even
20 is a even
35 is a odd
45 is a odd
50 is a even

  1. C ++ STLのcosh()関数

    cosh()関数は、ラジアンで指定された角度の双曲線正弦を返します。これはC++STLに組み込まれている関数です。 cosh()関数の構文は次のとおりです。 cosh(var) 構文からわかるように、関数cosh()は、データ型float、double、またはlongdoubleのパラメーターvarを受け入れます。 varの双曲線コサインを返します。 C ++でcosh()を示すプログラムは次のとおりです- 例 #include <iostream> #include <cmath> using namespace std; int main() {  

  2. C ++ STLのsinh()関数

    sinh()関数は、ラジアンで指定された角度の双曲線正弦を返します。これはC++STLに組み込まれている関数です。 sinh()関数の構文は次のとおりです。 sinh(var) 構文からわかるように、関数sinh()は、データ型float、double、またはlongdoubleのパラメーターvarを受け入れます。 varの双曲線サインを返します。 C ++でsinh()を示すプログラムは次のとおりです。 例 #include <iostream> #include <cmath> using namespace std; int main() {