I've written the following binary search program using template.
But when I run it, it gives an error: 55|error: no matching function for call to ‘Binary_Search(std::vector<int>&, int&, int&)’|
#include<iostream>
#include<vector>
usingnamespace std;
template<class T>
int Binary_Search(T& Array, T& Size, T& Value)
{
int Start=0;
int End=Size;
int Mid;
while(Start<=End)
{
Mid=(Start+End)/2;
if(Array[Mid]==Value)
return Mid;
elseif(Array[Mid]<Value)
Start=Mid+1;
else
End=Mid-1;
return -1;
}
}
int main()
{
vector<int>Array;
cout<<"Enter number of elements: ";
int n;
cin>>n;
cout<<"Enter the elements: ";
for(int i=0; i<n; i++)
{
int element;
cin>>element;
Array.push_back(element);
}
cout<<"Enter the value you want to search: ";
int Value;
cin>>Value;
int Size=Array.size();
if(Binary_Search(Array,Size,Value)==-1)
cout<<"Element not found!"<<endl;
else
cout<<"Element found at position "<<Binary_Search(Array,Size,Value)<<endl;
return 0;
}