ArrayCompare

この関数は 2 つの同型の配列を比較した結果を返します。基本データ型 または 複合型オブジェクトを使用しないカスタム構造体、つまり文字列型動的配列、クラスや他の複合オブジェクトで出来た構造体を含まないカスタム構造体です。

int  ArrayCompare(
  const void&  array1[],            // 1番目の配列
  const void&  array2[],            // 2 番目の配列
  int          start1=0,            // 1番目の配列の初期オフセット
  int          start2=0,            // 2 番目の配列の初期オフセット
  int         count=WHOLE_ARRAY   // 比較される要素の数
  );

パラメータ

array1[]

[in]  1番目の配列

array2[]

[in]  2 番目の配列

start1=0

[in]  比較開始点となる、1番目の配列内の要素の初期インデックス。デフォルトの開始インデックスは0です。

start2=0

[in]  比較開始点となる、2 番目の配列内の要素の初期インデックス。デフォルトの開始インデックスは0です。

count=WHOLE_ARRAY

[in]  比較される要素の数デフォルトでは両方の配列の全ての要素が比較されます(count=WHOLE_ARRAY)。

戻り値

  • -1( array1[] が array2[] より小さい)
  • 0( array1[] とarray2[] が等しい)
  • 1( array1[] が array2[] より大きい)
  • -2(両配列の型の非互換性や start1、start2 または count 値が配列の範囲外にあることによってエラーが発生)

注意事項

配列のサイズが異なり、いずれかの配列がもう 1 つの配列の忠実なサブセットであって、かつ count= WHOLE_ARRAY である場合、0 が返されず、両配列は等しいとみなされません。この場合、両配列のサイズ比較による戻り値は、array1[] のサイズが array2[] のサイズより小さい場合は -1 でそれ以外は1になります。

Example:

//--- global variables
double   ExtArrayFirst[];
double   ExtArraySecond[];
 
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
 {
//--- set the array sizes
  if(ArrayResize(ExtArrayFirst,10)!=10)
    {
    Print("ArrayResize() failed for ExtArrayFirst. Error code: ",GetLastError());
    return;
    }
  if(ArrayResize(ExtArraySecond,10)!=10)
    {
    Print("ArrayResize() failed for ExtArraySecond. Error code: ",GetLastError());
    return;
    }
   
//--- fill the arrays with the values of i and j indices in a loop
  int total=ArraySize(ExtArrayFirst);
  for(int i=0, j=total-1; i<total; i++,j--)
    {
    //--- fill the ExtArrayFirst array from left to right
    //--- fill the ExtArraySecond array from right to left
    ExtArrayFirst[i]=i;
    ExtArraySecond[i]=j;
    }
//--- compare the arrays and print the result in the log
  ArrayComparePrint(ExtArrayFirst,ExtArraySecond);
  /*
  Result:
  ExtArrayFirst:
  0.00000 1.00000 2.00000 3.00000 4.00000 5.00000 6.00000 7.00000 8.00000 9.00000
  ExtArraySecond:
  9.00000 8.00000 7.00000 6.00000 5.00000 4.00000 3.00000 2.00000 1.00000 0.00000
  Result ArrayCompare(): ExtArrayFirst is smaller than ExtArraySecond (result = -1)
  */
 
//--- now let's flip the arrays
//--- fill the arrays with the values of i and j indices in a loop
  for(int i=0, j=total-1; i<total; i++,j--)
    {
    //--- fill the ExtArrayFirst array from right to left
    //--- fill the ExtArraySecond array from left to right
    ExtArrayFirst[i]=j;
    ExtArraySecond[i]=i;
    }
//--- compare the arrays and print the result in the log
  ArrayComparePrint(ExtArrayFirst,ExtArraySecond);
  /*
  Result:
  ExtArrayFirst:
  9.00000 8.00000 7.00000 6.00000 5.00000 4.00000 3.00000 2.00000 1.00000 0.00000
  ExtArraySecond:
  0.00000 1.00000 2.00000 3.00000 4.00000 5.00000 6.00000 7.00000 8.00000 9.00000
  Result ArrayCompare(): ExtArrayFirst is larger than ExtArraySecond (result = 1)
  */
 
//--- now let's fill the arrays in one direction
//--- fill the arrays with the values of i index in a loop
  for(int i=0; i<total; i++)
    {
    //--- fill both arrays from left to right
    ExtArrayFirst[i]=i;
    ExtArraySecond[i]=i;
    }
//--- compare the arrays and print the result in the log
  ArrayComparePrint(ExtArrayFirst,ExtArraySecond);
  /*
  Result:
  ExtArrayFirst:
  0.00000 1.00000 2.00000 3.00000 4.00000 5.00000 6.00000 7.00000 8.00000 9.00000
  ExtArraySecond:
  0.00000 1.00000 2.00000 3.00000 4.00000 5.00000 6.00000 7.00000 8.00000 9.00000
  Result ArrayCompare(): ExtArrayFirst and ExtArraySecond are equal (result = 0)
  */
 }
//+------------------------------------------------------------------+
//| Compare and display the result                                   |
//+------------------------------------------------------------------+
void ArrayComparePrint(const double &array1[], const double &array2[])
 {
  //--- print the header and contents of the arrays
  Print("ExtArrayFirst:");
  ArrayPrint(array1);
  Print("ExtArraySecond:");
  ArrayPrint(array2);
  //--- compare the arrays and print the comparison result
  int   res=ArrayCompare(array1,array2);
  string res_str=(res>0 ? "ExtArrayFirst is larger than ExtArraySecond" : res<0 ? "ExtArrayFirst is smaller than ExtArraySecond" : "ExtArrayFirst and ExtArraySecond are equal");
  PrintFormat("Result ArrayCompare(): %s (result = %d)\n",res_str,res);
 }
//+------------------------------------------------------------------+