std::binary_search
![]() |
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 <algorithm>
|
||
template< class ForwardIt, class T > bool binary_search( ForwardIt first, ForwardIt last, const T& value ); |
(1) | |
template< class ForwardIt, class T, class Compare > bool binary_search( ForwardIt first, ForwardIt last, const T& value, Compare comp ); |
(2) | |
[first, last)
ordenados contiene un elemento igual a value
. La primera versión utiliza operator< para comparar los elementos, la segunda versión utiliza la función de comparación dado comp
.[first, last)
contains an element equal to value
. The first version uses operator< to compare the elements, the second version uses the given comparison function comp
.You can help to correct and verify the translation. Click here for instructions.
Contenido |
[editar] Parámetros
first, last | - | la gama de elementos a examinar
Original: the range of elements to examine The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
value | - | valor para comparar los elementos a
Original: value to compare the elements to The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
comp | - | objeto función de comparación (es decir, un objeto que satisface los requerimientos de Compare) que devuelve true si el primer argumento es menor que el segundo. La signatura de la función de comparación deberá ser equivalente a lo siguiente: bool cmp(const Type1 &a, const Type2 &b); Mientras que la signatura no necesita ser const &, la función no debe modificar los objetos que se le pasaron y debe admitir todos los valores de los tipos (posiblemente |
Requerimientos de tipo | ||
-ForwardIt debe reunir los requerimientos de ForwardIterator .
|
[editar] Valor de retorno
value
se encuentra, por lo demás false .value
is found, false otherwise.You can help to correct and verify the translation. Click here for instructions.
[editar] Complejidad
first
y last
first
and last
You can help to correct and verify the translation. Click here for instructions.
[editar] Posible implementación
Primera versión |
---|
template<class ForwardIt, class T> bool binary_search(ForwardIt first, ForwardIt last, const T& value) { first = std::lower_bound(first, last, value); return (first != last && !(value < *first)); } |
Segunda versión |
template<class ForwardIt, class T, class Compare> bool binary_search(ForwardIt first, ForwardIt last, const T& value, Compare comp) { first = std::lower_bound(first, last, value); return (first != last && !(comp(value, *first)); } |
[editar] Ejemplo
#include <iostream> #include <algorithm> #include <vector> int main() { std::vector<int> haystack {1, 3, 4, 5, 9}; std::vector<int> needles {1, 2, 3}; for (auto needle : needles) { std::cout << "Searching for " << needle << '\n'; if (std::binary_search(haystack.begin(), haystack.end(), needle)) { std::cout << "Found " << needle << '\n'; } else { std::cout << "no dice!\n"; } } }
Salida:
Searching for 1 Found 1 Searching for 2 no dice! Searching for 3 Found 3
[editar] Ver también
Devuelve el rango de los elementos que coinciden con una clave específica 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. (plantilla de función) |