最終更新日時(UTC):
が更新

履歴 編集

function template
<algorithm>

std::ranges::binary_search(C++20)

namespace std::ranges {
  template <forward_iterator I,
            sentinel_for<I> S,
            class T,
            class Proj = identity,
            indirect_strict_weak_order<
              const T*,
              projected<I, Proj>
            > Comp = ranges::less>
  constexpr bool
    binary_search(I first,
                  S last,
                  const T& value,
                  Comp comp = {},
                  Proj proj = {}); // (1) C++20
  template <forward_iterator I,
            sentinel_for<I> S,
            class Proj = identity,
            class T = projected_value_t<I, Proj>,
            indirect_strict_weak_order<
              const T*,
              projected<I, Proj>
            > Comp = ranges::less>
  constexpr bool
    binary_search(I first,
                  S last,
                  const T& value,
                  Comp comp = {},
                  Proj proj = {}); // (1) C++26

  template <forward_range R,
            class T,
            class Proj = identity,
            indirect_strict_weak_order<
              const T*,
              projected<iterator_t<R>, Proj>
            > Comp = ranges::less>
  constexpr bool
    binary_search(R&& r,
                  const T& value,
                  Comp comp = {},
                  Proj proj = {}); // (2) C++20
  template <forward_range R,
            class Proj = identity,
            class T = projected_value_t<iterator_t<R>, Proj>,
            indirect_strict_weak_order<
              const T*,
              projected<iterator_t<R>, Proj>
            > Comp = ranges::less>
  constexpr bool
    binary_search(R&& r,
                  const T& value,
                  Comp comp = {},
                  Proj proj = {}); // (2) C++26
}

概要

二分探索法による検索を行う。

事前条件

[first,last) の要素 ee < value および !(value < e)、または comp(e, value) および !comp(value, e) によって区分化されていなければならない。

また、[first, last) の全ての要素 e は、e < value であれば !(value < e) である、または comp(e, value) であれば !comp(value, e) である必要がある。

戻り値

[first,last) 内のイテレータ i について、!(*i < value) && !(value < *i) または comp(*i, value) == false && comp(value, *i) == false であるようなイテレータが見つかった場合は true を返す。

計算量

最大で log2(last - first) + O(1) 回の比較を行う

備考

  • comp は 2 引数の関数オブジェクトで、1 番目の引数が 2 番目の引数「より小さい」場合に true を、そうでない場合に false を返すものとして扱われる。
  • (1), (2) :
    • C++26 : 引数として波カッコ初期化{}を受け付ける
      std::vector<T> v;
      bool found = std::ranges::binary_search(v, {a, b});
      

基本的な使い方

#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
  // binary_search で 4 を検索する場合、
  // 4 より小さい物、4 と等しい物、4 より大きい物がその順に並んでいれば、
  // 必ずしもソートされている必要はない。
  std::vector<int> v = {3, 1, 4, 6, 5};

  if (std::ranges::binary_search(v, 4)) {
    std::cout << "found" << std::endl;
  }
  else {
    std::cout << "not found" << std::endl;
  }
}

出力

found

波カッコ初期化を入力として使用する (C++26)

#include <algorithm>
#include <iostream>
#include <vector>

struct Point {
  int x;
  int y;

  bool operator==(const Point& other) const = default;
  auto operator<=>(const Point& other) const = default;
};

int main() {
  std::vector<Point> v = {
    {1, 2},
    {3, 4},
    {5, 6},
  };

  // 値{3, 4}を二分検索
  bool found = std::ranges::binary_search(v, {3, 4});
  if (found) {
    std::cout << "found" << std::endl;
  }
  else {
    std::cout << "not found" << std::endl;
  }
}

出力

found

バージョン

言語

  • C++20

処理系

参照