The Wayback Machine - https://web.archive.org/web/20171215161708/http://it.cppreference.com:80/w/cpp/algorithm/bsearch

std::bsearch

Da cppreference.com.
< cpp‎ | algorithm

 
 
Algoritmo libreria
Funzioni
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Non modifica le operazioni di sequenza
Original:
Non-modifying sequence operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Modifica delle operazioni di sequenza
Original:
Modifying sequence operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Partizionamento operazioni
Original:
Partitioning operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Ordinamento delle operazioni (su intervalli ordinati)
Original:
Sorting operations (on sorted ranges)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Binarie (le operazioni di ricerca sui campi ordinati)
Original:
Binary search operations (on sorted ranges)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Impostare le operazioni (su intervalli ordinati)
Original:
Set operations (on sorted ranges)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Heap operazioni
Original:
Heap operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Minimo / massimo le operazioni
Original:
Minimum/maximum operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Operazioni numeriche
Original:
Numeric operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Libreria C
Original:
C library
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
bsearch
 
Defined in header <cstdlib>
void* bsearch(constvoid* key, constvoid* ptr, size_t count, size_t size,
               int(*comp)(constvoid*, constvoid*));
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.

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. 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(constvoid*a, constvoid*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.

[modifica]Esempio

#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);}

Output:

p1: 0xbf9a4c88 4 p2: NULL

[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)[edit]
torna gamma di elementi che corrispondono un tasto specifico
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.

(funzione di modello)[edit]
C documentation for bsearch
close