1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
template <class ForwardIterator, class T>
ForwardIterator lower_ (ForwardIterator first, ForwardIterator last, const T& val)
{
ForwardIterator it;
typename iterator_traits<ForwardIterator>::difference_type count, step;
count = distance(first,last);
while (count>0)
{
it = first; step=count/2; advance (it,step);
if (*it<val) { // or: if (comp(*it,val)), for version (2)
first=++it;
count-=step+1;
}
else count=step;
}
return first;
}
template <class ForwardIterator, class T>
bool binary_srch (ForwardIterator first, ForwardIterator last, const T& val)
{
first = lower_(first,last,val);
return (first!=last && !(val<*first));
}
// Driver program to test above function
int main()
{
vector<string>v;
v.push_back("wefer");
v.push_back("SDAAFS");
v.push_back("ASDFWEaefv");
v.push_back("afea");
v.push_back("jnhfe");
cout<<binary_srch(v.begin(),v.end(),"jhg");
return 0;
}
|