bsearch
Da cppreference.com.
![]() |
Questa pagina è stata tradotta in modo automatico dalla versione in ineglese della wiki usando Google Translate.
La traduzione potrebbe contenere errori e termini strani. Muovi il puntatore sopra al testo per vedere la versione originale. Puoi aiutarci a correggere gli gli errori. Per ulteriori istruzioni clicca qui. |
Elemento definito nell'header <stdlib.h>
|
||
Trova un elemento pari a elemento puntato da
key
in un array puntato da ptr
. La matrice contiene elementi count
di size
dimensioni. Funzione puntato da comp
viene utilizzato per confronto oggetto.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.
Indice |
[modifica] Parametri
key | - | puntatore all'elemento da ricercare
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 | - | puntatore alla matrice da esaminare
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 | - | numero di elemento della matrice
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 | - | dimensione di ciascun elemento della matrice in byte
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(const void *a, const void *b); The function must not modify the objects passed to it. |
[modifica] Valore di ritorno
puntatore all'elemento trovato o NULL altrimenti.
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.
[modifica] Esempio
#include <stdlib.h> #include <stdio.h> struct data { int nr; char const *value; } dat[] = { {1, "Foo"}, {2, "Bar"}, {3, "Hello"}, {4, "World"} }; int data_cmp(void const *lhs, void const *rhs) { struct data const *const l = lhs; struct data const *const r = rhs; return l->nr < r->nr; } int main(void) { struct data key = { .nr = 3 }; struct data const *res = bsearch(&key, dat, sizeof(dat)/sizeof(dat[0]), sizeof(dat[0]), data_cmp); if(!res) { printf("No %d not found\n", key.nr); } else { printf("No %d: %s\n", res->nr, res->value); } }
Output:
No 3: Hello
[modifica] Vedi anche
Ordina un intervallo di elementi di tipo non specificato 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. (funzione) | |
C++ documentation for bsearch
|