std::lower_bound( )の最初のステップとして簡単な比較がないのはなぜですか?
最初のステップでstd::lower_bound
イテレータがリスト内から中央の位置に変更it
さfirst
れると、次のようになります。
step = std::distance(first,last) / 2;
it = std::advance(first, step);
その後、アルゴリズムはその center-it
と givenの比較を開始しvalue
ます。
if (*it < value) { ... } else { ... }
しかし、些細なケースは、再配置する前の最初のステップとして 1 つの比較を行うことit
です。
if (value < *first) return last;
// else start original algorithm ...
std::lower_bound
非常に長いリストがあり、現在の形で が実現するまで待つ必要があると想像してください。これvalue < *first
は本当です。確かにO( log_2( last - first ) )ですが、そのような場合はO(1)に 1 行だけ追加される可能性があります。