std::bsearch – cppreference.com
Aus cppreference.com
<metanoindex/>
<tbody> </tbody>
| definiert in Header <cstdlib> |
||
|
|
||
Findet ein Element gleich Element, auf das key in einem Array, auf das ptr. Das Array enthält count Elemente der Größe size. Funktion, auf die comp ist für Objekt Vergleich herangezogen .
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.
Parameter
| key | - | Zeiger auf das Element zu suchen Original: pointer to the element to search for The text has been machine-translated via Google Translate. |
| ptr | - | Zeiger auf das Array zu prüfen Original: pointer to the array to examine The text has been machine-translated via Google Translate. |
| count | - | Zahl der Element in dem Array Original: number of element in the array The text has been machine-translated via Google Translate. |
| size | - | Größe der einzelnen Elemente in dem Array in Bytes Original: size of each element in the array in bytes The text has been machine-translated via Google Translate. |
| 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.
The function must not modify the objects passed to it. |
Rückgabewert
Pointer auf die gefundene Element oder NULL sonst .
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.
Beispiel
#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); }
Output:
p1: 0xbf9a4c88 4 p2: NULL
Siehe auch
Sortiert einen Bereich von Elementen mit nicht angegebenen Typ Original: sorts a range of elements with unspecified type The text has been machine-translated via Google Translate. (Funktion) [edit] | |
Bandbreite der Rendite von Elementen übereinstimmenden eine bestimmte Taste Original: returns range of elements matching a specific key The text has been machine-translated via Google Translate. (Funktions-Template) [edit] | |
C documentation for bsearch | |