Namensräume
Varianten
Aktionen

bsearch

Aus cppreference.com
< c‎ | algorithm

Defined in header <stdlib.h>
void* bsearch( const void* key, const void* ptr, size_t count, size_t size,
               int (*comp)(const void*, const void*) );
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.

Inhaltsverzeichnis

[Bearbeiten] 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.
You can help to correct and verify the translation. Click here for instructions.
ptr -
Zeiger auf das Array zu prüfen
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 -
Zahl der Element in dem Array
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 -
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.
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. key is passed as the first argument, an element from the array as the second.
The signature of the comparison function should be equivalent to the following:

 int cmp(const void *a, const void *b);

The function must not modify the objects passed to it.

[Bearbeiten] Rückgabewert

Zeiger auf das 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.

[Bearbeiten] Beispiel

#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

[Bearbeiten] 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.
You can help to correct and verify the translation. Click here for instructions.

(Funktion) [edit]
C++ documentation for bsearch