跳至内容

ArrayBsearch

ArrayBsearch

在按升序排列的多维数值数组中搜索指定的值。搜索是在第一个维度上进行的,同时考虑AS_SERIES标志

对于双精度类型数组的搜索

int  ArrayBsearch(
   const double&   array[],               // array for search
   double          value,                 // what is searched for
   int             count=WHOLE_ARRAY,     // count of elements to search for
   int             start=0,               // starting position
   int             direction=MODE_ASCEND  // search direction
   );

对于单精度类型数组的搜索

int  ArrayBsearch(
   const float&    array[],               // array for search
   float           value,                 // what is searched for
   int             count=WHOLE_ARRAY,     // count of elements to search for
   int             start=0,               // starting position
   int             direction=MODE_ASCEND  // search direction
   );

对于长整型数组的搜索

int  ArrayBsearch(
   const long&    array[],               // array for search
   long           value,                 // what is searched for
   int            count=WHOLE_ARRAY,     // count of elements to search for
   int            start=0,               // starting position
   int            direction=MODE_ASCEND  // search direction
   );

对于整型数组的搜索

int  ArrayBsearch(
   const int&    array[],               // array for search
   int           value,                 // what is searched for
   int           count=WHOLE_ARRAY,     // count of elements to search for
   int           start=0,               // starting position
   int           direction=MODE_ASCEND  // search direction
   );

对于短整型数组的搜索

int  ArrayBsearch(
   const short&   array[],               // array for search
   short          value,                 // what is searched for
   int            count=WHOLE_ARRAY,     // count of elements to search for
   int            start=0,               // starting position
   int            direction=MODE_ASCEND  // search direction
   );

对于字符型数组的搜索

int  ArrayBsearch(
   const char&    array[],               // array for search
   char           value,                 // what is searched for
   int            count=WHOLE_ARRAY,     // count of elements to search for
   int            start=0,               // starting position
   int            direction=MODE_ASCEND  // search direction
   );

参数

array[]

[in] 用于搜索的数值数组。

value

[in] 要搜索的值。

count=WHOLE_ARRAY

[in] 要搜索的元素数量。默认情况下,会在整个数组中搜索。

start=0

[in] 开始搜索的索引。默认情况下,搜索从第一个元素开始。

direction=MODE_ASCEND

[in] 搜索方向。可以是以下值之一:

MODE_ASCEND 向前搜索, MODE_DESCEND 向后搜索。

返回值

该函数返回找到的元素的索引。如果未找到所需的值,函数将返回最接近该值的元素的索引。

注意

二分搜索仅适用于已排序的数组。要对数值数组进行排序,请使用ArraySort()函数。

示例:

datetime daytimes[];
   int      shift=10,dayshift;
   // All the Time[] series are sorted in descendant mode
   ArrayCopySeries(daytimes,MODE_TIME,Symbol(),PERIOD_D1);
   if(Time[shift]>=daytimes[0]) dayshift=0;
   else
     {
      dayshift=ArrayBsearch(daytimes,Time[shift],WHOLE_ARRAY,0,MODE_DESCEND);
      if(Period()<PERIOD_D1) dayshift++;
     }
   Print(TimeToStr(Time[shift])," corresponds to ",dayshift," day bar opened at ",
         TimeToStr(daytimes[dayshift]));
最后更新于