Namespaces
Variants

std:: bsearch

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
bsearch
Numeric operations
Operations on uninitialized memory
ヘッダーで定義 <cstdlib>
void * bsearch ( const void * key, const void * ptr, std:: size_t count,

std:: size_t size, /* c-compare-pred */ * comp ) ;
void * bsearch ( const void * key, const void * ptr, std:: size_t count,

std:: size_t size, /* compare-pred */ * comp ) ;
(1)
extern "C" using /* c-compare-pred */ = int ( const void * , const void * ) ;
extern "C++" using /* compare-pred */ = int ( const void * , const void * ) ;
(2) ( 説明専用* )

key が指す要素と等しい要素を、 ptr が指す配列内から検索します。配列は count 個の要素を持ち、各要素のサイズは size バイトです。配列は key が指すオブジェクトに対して分割されていなければなりません。つまり、キーオブジェクトより小さいと比較されるすべての要素は、等しいと比較されるすべての要素の前に現れ、それらはキーオブジェクトより大きいと比較されるすべての要素の前に現れる必要があります。完全にソートされた配列はこれらの要件を満たします。要素は comp が指す関数を使用して比較されます。

配列が昇順で分割されていない場合の動作は未定義です。これは、 comp が使用するのと同じ基準に従います。

配列に検索対象の要素と comp によって等価と判定される複数の要素が含まれている場合、関数が結果としてどの要素を返すかは未規定です。

目次

パラメータ

key - 検索対象の要素へのポインタ
ptr - 調査対象の配列へのポインタ
count - 配列内の要素数
size - 配列の各要素のサイズ(バイト単位)
comp - 比較関数。第1引数が第2引数より 小さい 場合は負の整数値、第1引数が第2引数より 大きい 場合は正の整数値、引数が等価の場合はゼロを返す。 key が第1引数として、配列の要素が第2引数として渡される。

比較関数のシグネチャは以下と同等であるべき:

int cmp ( const void * a, const void * b ) ;

この関数は渡されたオブジェクトを変更してはならず、配列内の位置に関係なく同じオブジェクトに対して呼び出された場合に一貫した結果を返さなければならない。

戻り値

見つかった要素へのポインタ、または要素が見つからなかった場合は null ポインタ。

注記

名前にもかかわらず、C言語およびPOSIX標準は、この関数が二分探索を用いて実装されることや、複雑性に関する保証を一切要求していません。

C++標準ライブラリが提供する2つのオーバーロードは、パラメータ comp の型が異なるため区別されます( language linkage はその型の一部です)。

#include <array>
#include <cstdlib>
#include <iostream>
template<typename T>
int compare(const void *a, const void *b)
{
    const auto &arg1 = *(static_cast<const T*>(a));
    const auto &arg2 = *(static_cast<const T*>(b));
    const auto cmp = arg1 <=> arg2;
    return cmp < 0 ? -1
        :  cmp > 0 ? +1
        :  0;
}
int main()
{
    std::array arr{1, 2, 3, 4, 5, 6, 7, 8};
    for (const int key : {4, 8, 9})
    {
        const int* p = static_cast<int*>(
            std::bsearch(&key,
                arr.data(),
                arr.size(),
                sizeof(decltype(arr)::value_type),
                compare<int>));
        std::cout << "value " << key;
        if (p)
            std::cout << " found at position " << (p - arr.data()) << '\n';
        else
            std::cout << " not found\n";
    }
}

出力:

value 4 found at position 3
value 8 found at position 7
value 9 not found

関連項目

未指定の型の要素範囲をソートする
(関数)
特定のキーに一致する要素の範囲を返す
(関数テンプレート)