Inhaltsverzeichnis

binary_search()

#include <algorithm>

bool binary_search (For first, For last, T wert) 
bool binary_search (For first, For last, T wert, Binary comp) 
 
bool ranges::binary_search (Range r, T wert, Binary comp = {}, Proj proj = {}) 
bool ranges::binary_search (For first, For last, T wert, Binary comp = {}, Proj proj = {}) 

Prüft, ob wert (als Projektion) in der aufsteigend geordneten Folge [first,last) enthalten ist.

Parameter

first Anfang des Bereiches
last Ende des Bereiches
wert gesuchter Wert
comp Sortierkriterium (Vorgabe = less)
proj einstelliger Funktor (Vorgabe = std::identity)

Ergebnis

Rückgabewert: true, wenn wert gefunden wurde bzw. wenn es einen Iterator i im Bereich [first,last) gibt, für den gilt:

comp(proj(*i), value) == false && comp(value, proj(*i)) == false

Siehe auch

bsearch(), find(), equal_range(), lower_bound(), upper_bound().

Beispiel

binary_search.cpp
#include <algorithm>
#include <iostream>
 
int main()
{
  double arr[] = { 1, 2, 3, 4, 5 };
 
  std::cout << std::binary_search(arr, arr+5, 2) << '\n';
}