#include <iostream>
#include <vector>
#include <stdlib.h>
using namespace std;
template <typename T>
/*int binarySearch(const vector<T> table, int bot, int top, const T &x) // find the last one
{
int b = bot, t = top, mid;
int find = -1;
while (b <= t)
{
mid = (b+t)/2;
if (table[mid] == x) find = mid;
if (table[mid] <= x) b = mid+1;
else t = mid - 1;
}
return find;
}*/
int binarySearch(const vector<T> table, int bot, int top, const T &x) // find the first one
{
int b = bot, t = top, mid;
int find = -1;
while (b <= t)
{
mid = (b+t)/2;
if (table[mid] == x) find = mid;
if (table[mid] < x) b = mid+1;
else t = mid - 1;
}
return find;
}
int main ()
{
int a[ ] = {0,1,1,3,3,3,6};
int b[] = {1};
//vector<int> v(a,a+7);
vector<int> v(a,a+7);
cout << binarySearch(v, 0, v.size()-1, 1) << endl; // ??5
cout << binarySearch(v, 0, v.size()-1, 3) << endl;
cout << binarySearch(v, 0, v.size()-1, 7) << endl; //??-1
system("pause");
return 0;
}
#include <vector>
#include <stdlib.h>
using namespace std;
template <typename T>
/*int binarySearch(const vector<T> table, int bot, int top, const T &x) // find the last one
{
int b = bot, t = top, mid;
int find = -1;
while (b <= t)
{
mid = (b+t)/2;
if (table[mid] == x) find = mid;
if (table[mid] <= x) b = mid+1;
else t = mid - 1;
}
return find;
}*/
int binarySearch(const vector<T> table, int bot, int top, const T &x) // find the first one
{
int b = bot, t = top, mid;
int find = -1;
while (b <= t)
{
mid = (b+t)/2;
if (table[mid] == x) find = mid;
if (table[mid] < x) b = mid+1;
else t = mid - 1;
}
return find;
}
int main ()
{
int a[ ] = {0,1,1,3,3,3,6};
int b[] = {1};
//vector<int> v(a,a+7);
vector<int> v(a,a+7);
cout << binarySearch(v, 0, v.size()-1, 1) << endl; // ??5
cout << binarySearch(v, 0, v.size()-1, 3) << endl;
cout << binarySearch(v, 0, v.size()-1, 7) << endl; //??-1
system("pause");
return 0;
}