I think you can have to go though the following code of lines that can describes you what is the use and how to implement the binary_search() function in the C++ programming :
Code:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
bool mfunction (int i,int j)
{
return (i<j);
}
int main ()
{
int mints[] = {1,2,3,4,5,4,3,2,1};
wector<int> w(mints,mints+9);
sort (w.begin(), w.end());
cout << "looking for a 3... ";
if (binary_search (w.begin(), w.end(), 3))
cout << "found!\n"; else cout << "not found.\n";
sort (w.begin(), w.end(), mfunction);
cout << "looking for a 6... ";
if (binary_search (w.begin(), w.end(), 6, mfunction))
cout << "found!\n"; else cout << "not found.\n";
return 0;
}
Output:
Code:
looking for a 3... found!
looking for a 6... not found.
Bookmarks