std::bsearch
De cppreference.com
![]() | This page has been machine-translated from the English version of the wiki using Google Translate. The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
Déclaré dans l'en-tête <cstdlib> | ||
void* bsearch(constvoid* key, constvoid* ptr, size_t count, size_t size, int(*comp)(constvoid*, constvoid*)); | ||
Recherche un élément égal à l'élément pointé par
key
dans un tableau pointé par ptr
. Le tableau contient des éléments count
de size
taille. Fonction pointée par comp
est utilisé pour la comparaison d'objets .Original:
Finds an element equal to element pointed to by
key
in an array pointed to by ptr
. The array contains count
elements of size size
. Function pointed to by comp
is used for object comparison.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Sommaire |
[modifier]Paramètres
key | - | pointeur vers l'élément à rechercher Original: pointer to the element to search for The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
ptr | - | pointeur vers le tableau d'examiner Original: pointer to the array to examine The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
count | - | nombre d'éléments dans le tableau Original: number of element in the array The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
size | - | taille de chaque élément dans le tableau en octets Original: size of each element in the array in bytes The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
comp | - | comparison function which returns a negative integer value if the first argument is less than the second, a positive integer value if the first argument is greater than the second and zero if the arguments are equal. int cmp(constvoid*a, constvoid*b); The function must not modify the objects passed to it. |
[modifier]Retourne la valeur
Pointeur vers l'élément trouvé ou NULL autrement .
Original:
Pointer to the found element or NULL otherwise.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[modifier]Exemple
#include <cstdlib>#include <iostream> int compare(constvoid*ap, constvoid*bp){constint*a =(int*) ap;constint*b =(int*) bp;return*a -*b;} int show_ptr(int*p){if(p ==NULL){std::cout<<"NULL\n";}else{std::cout<< p1 <<' '<<*p1 <<'\n';}} int main(int argc, char**argv){constint ARR_SIZE =8;int arr[ARR_SIZE]={1, 2, 3, 4, 5, 6, 7, 8}; int key1 =4;int*p1 =(int*) std::bsearch(&key1, arr, ARR_SIZE, sizeof(arr[0]), compare); int key2 =9;int*p2 =(int*) std::bsearch(&key2, arr, ARR_SIZE, sizeof(arr[0]), compare); std::cout<<"p1: "; show_ptr(p1); std::cout<<"p2: "; show_ptr(p2);}
Résultat :
p1: 0xbf9a4c88 4 p2: NULL
[modifier]Voir aussi
Trie une plage d'éléments de type indéterminé Original: sorts a range of elements with unspecified type The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (fonction) | |
rendements varient d'éléments correspondant à une clé spécifique Original: returns range of elements matching a specific key The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (fonction générique) | |
C documentation for bsearch |