Ezruff
0
Q:

binary search stl

// CPP program to implement 
// Binary Search in  
// Standard Template Library (STL) 
#include <algorithm> 
#include <iostream> 
  
using namespace std; 
  
void show(int a[], int arraysize) 
{ 
    for (int i = 0; i < arraysize; ++i) 
        cout << a[i] << " "; 
} 
  
int main() 
{ 
    int a[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 }; 
    int asize = sizeof(a) / sizeof(a[0]); 
    cout << "\n The array is : "; 
    show(a, asize); 
  
    cout << "\n\nLet's say we want to search for 2 in the array"; 
    cout << "\n So, we first sort the array"; 
    sort(a, a + asize); 
    cout << "\n\n The array after sorting is : "; 
    show(a, asize); 
    cout << "\n\nNow, we do the binary search"; 
    if (binary_search(a, a + 10, 2)) 
        cout << "\nElement found in the array"; 
    else
        cout << "\nElement not found in the array"; 
  
    cout << "\n\nNow, say we want to search for 10"; 
    if (binary_search(a, a + 10, 10)) 
        cout << "\nElement found in the array"; 
    else
        cout << "\nElement not found in the array"; 
  
    return 0; 
} 
2
// C++ code to demonstrate the working of binary_search() 
  
#include<bits/stdc++.h>  
using namespace std; 
  
int main() 
{ 
    // initializing vector of integers 
    vector<int> arr = {10, 15, 20, 25, 30, 35}; 
      
    // using binary_search to check if 15 exists 
    if (binary_search(arr.begin(), arr.end(), 15)) 
       cout << "15 exists in vector"; 
    else 
       cout << "15 does not exist"; 
       
    cout << endl; 
      
    // using binary_search to check if 23 exists 
    if (binary_search(arr.begin(), arr.end(), 23)) 
         cout << "23 exists in vector"; 
    else 
         cout << "23 does not exist"; 
       
    cout << endl;     
} 
1
binary_search(startaddress, endaddress, valuetofind)

startaddress: the address of the first element of the array.
endaddress: the address of the last element of the array.
valuetofind: the target value which we have to search for.
2

New to Communities?

Join the community