std::bsearch
De cppreference.com
![]() |
Esta página se ha traducido por ordenador/computador/computadora de la versión en inglés de la Wiki usando Google Translate.
La traducción puede contener errores y palabras aparatosas/incorrectas. Planea sobre el texto para ver la versión original. Puedes ayudar a corregir los errores y mejorar la traducción. Para instrucciones haz clic aquí. |
Definido en el archivo de encabezado <cstdlib>
|
||
void* bsearch( const void* key, const void* ptr, size_t count, size_t size, int (*comp)(const void*, const void*) ); |
||
Busca un elemento igual al elemento al que apunta
key
en un array apuntado por ptr
. La matriz contiene elementos count
de size
tamaño. Función a la que apunta comp
se utiliza para la comparación de objetos .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.
Contenido |
[editar] Parámetros
key | - | puntero al elemento a buscar
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 | - | puntero a la matriz a examinar
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 | - | número de elemento de la matriz
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 | - | tamaño de cada elemento de la matriz en bytes
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 | - | función de comparación que devuelve un valor entero negativo si el primer argumento es menor que el segundo, devuelve un valor entero positivo si el primer argumento es mayor que el segundo y cero si los argumentos son iguales. key is passed as the first argument, an element from the array as the second.La identificación de la función de comparación debe ser equivalente a la siguiente: int cmp(const void *a, const void *b); La función no debe modificar los objetos que se le pasan y debe devolver resultados consistentes cuando se le piden los mismos objetos, independientemente de su posición en la matriz. |
[editar] Valor de retorno
Puntero al elemento encontrado o NULL de otra manera .
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.
[editar] Ejemplo
Ejecuta este código
#include <cstdlib> #include <iostream> int compare(const void *ap, const void *bp) { const int *a = (int *) ap; const int *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) { const int 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); }
Salida:
p1: 0xbf9a4c88 4 p2: NULL
[editar] Ver también
Ordena un intervalo de elementos con tipo no especificado. (función) | |
Devuelve el rango de los elementos que coinciden con una clave específica. (plantilla de función) | |
Documentación de C para bsearch
|