System.Generics.Collections.TList.BinarySearch

De RAD Studio API Documentation
Aller à : navigation, rechercher

Delphi

function BinarySearch(const Item: T; out Index: Integer): Boolean; overload;
function BinarySearch(const Item: T; out Index: Integer; const AComparer: IComparer<T>): Boolean; overload;

C++

bool __fastcall BinarySearch(const T Item, /* out */ int &Index)/* overload */;
bool __fastcall BinarySearch(const T Item, /* out */ int &Index, const System::DelphiInterface<System::Generics::Defaults::IComparer__1<T> > AComparer)/* overload */;

Propriétés

Type Visibilité  Source Unité  Parent
function public
System.Generics.Collections.pas
System.Generics.Collections.hpp
System.Generics.Collections TList

Description

Recherche un élément dans une liste triée en utilisant une recherche binaire.

La méthode surchargée BinarySearch recherche l'élément de liste Item en utilisant une recherche binaire. La méthode renvoie true si elle trouve l'élément, et false sinon. En cas de recherche fructueuse, Index contient l'index de base zéro de l'élément Item. En cas de recherche infructueuse, Index contient l'index de la première entrée supérieure à Item.

Remarque :  BinarySearch nécessite que la liste soit triée. La méthode IndexOf ne nécessite pas une liste triée, mais est généralement plus lente que BinarySearch.

Si plusieurs éléments de la liste correspondent à Item, l'index de la première occurrence est renvoyé dans Index. Il s'agit de l'index d'un des éléments correspondants, pas nécessairement le premier.

La fonction de comparaison AComparer peut être fournie pour comparer les éléments.

Si Item est hors de la portée de la liste, une exception EArgumentOutOfRangeException est déclenchée.

C'est une opération O(n) pour une liste avec n entrées.

Voir aussi