brunnel
0
Q:

binary search in set c++

// 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