php - 无法理解 C++ STL 源中的 1 行代码 : Lower_Bound/Upper_Bound

标签 php c++ stl binary-search lower-bound

我正在编写一些代码来查找其值不超过 PHP 给定整数的最后一个键。
例如,数组(0=>1,1=>2,2=>3,3=>3,4=>4)。给定整数 3,我将找到键 3。(二分查找)

然后在网上找了一些关于二分查找的引用资料。
我找到了这个,就是找到第一个值不小于C++给定整数的键。
它说:

template <class _ForwardIter, class _Tp, class _Distance>
_ForwardIter __lower_bound(_ForwardIter __first, _ForwardIter __last,
                           const _Tp& __val, _Distance*) 
{
  _Distance __len = 0;
  distance(__first, __last, __len);
  _Distance __half;
  _ForwardIter __middle;

  while (__len > 0) {
    __half = __len >> 1;
    __middle = __first;
    advance(__middle, __half);
    if (*__middle < __val) {
      __first = __middle;
      ++__first;
      __len = __len - __half - 1;
    }
    else
      __len = __half;        //    <======this line
  }
  return __first;
}

那么,为什么要使用“__len = __half;”而不是“__len = __half + 1;”?
在这个二进制搜索过程中,“_middle”在每个循环中所指的键/值不会被遗忘和丢失吗?
我的意思是,这两个“__len”似乎不会加起来等于完整的“__len”,似乎 __middle 已被跳过

附言: 我的原始问题的 PHP 代码是:

$cid_start  = $count - 1;
$len        = $count;
while($len > 0){
    $half   = $len >> 1;
    $middle = $cid_start - $half;
    if($c_index[$middle][1] > $time_start){
        $cid_start = $middle - 1;
        $len       = len       - $half - 1;
    }else{
        $len       = $half + 1;
    }
}

它会起作用吗?还是会出错?
当我在数组中找不到任何内容时,如何获得 -1 或其他结果?

最佳答案

二分查找的算法非常简单。

/**
 * Search $value in $array
 * Return the position in $array when found, -1 when not found
 * $array has numeric consecutive keys (0..count($array)-1)
 * $array is sorted ascending; this condition is mandatory for binary search
 * if $array is not sorted => the output is rubbish
 */
function search($value, array $array)
{
    // At each step search between positions $start and $end (including both)
    $start = 0;
    $end   = count($array) - 1;

    // End when the search interval shrunk to nothing
    while ($start <= $end) {
        // Get the middle of the interval
        // This is shorter and faster than intval(($start + $end) / 2)
        $middle = ($start + $end) >> 1;

        // Check the value in the middle of the current search interval
        if ($value == $array[$middle]) {
            // Found
            return $middle;
        }

        // Not found yet; the binary step: choose a direction
        if ($value < $array[$middle]) {
            // Search in the left half
            $end = $middle - 1;
        } else {
            // Search in the right half
            $start = $middle + 1;
        }
    }

    // Not found
    return -1;
}

关于php - 无法理解 C++ STL 源中的 1 行代码 : Lower_Bound/Upper_Bound,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27322112/

相关文章:

php - 对使用 proc_open 打开的进程进行多次写入

c++ - C++函数/方法设计的良好实践

c++ - 使用 std::deque 时重定位赋值

具有值语义且没有分配器恶作剧的 C++ 数组?

php - 使用 UNION ALL SQL 查询获取更多值

php - 使用 Amazon SES sendRawEmail 进行电子邮件编码

c++ - 工厂 : how to pass temporary smart pointers to functions. C++

c# - 如何将带有 unsigned char* 的结构从 C# 传递到 C++?

c++ - 进入 Xcode 5 中的 STL 源代码

php - 在 Ruby 和 PHP 之间共享 session