I have the following exercise:
Implement the int bsearch method (int i, int d, const T & x) of the vector_t class that performs the recursive binary search in an ordered vector, returning the position of the found element and -1 if it does not find it
The class they give me as help is this:
template <class T>
class vector_t{
private:
T* v_;
int sz_;
public:
vector_t(int sz);
~vector_t(void);
int get_sz(void) const;
T get_v(int pos) const;
T& get_set_v(int pos);
};
I have changed it BBR (int v [], int i, int d, int x);
template<class T>
int vector_t:: bsearch (int i, int d, const T& x){
if (i>d){
return -1;
}
int medio=(i+d)/2;
if(v_[medio]==x){
return medio;
}
else if(x<v_[medio]){
return bsearch(i,medio-1,x);
}
else if(x>v_[medio]){
return bsearch(medio+1,d,x);
}
}
I did the main too:
#include<iostream>
#include"SinNombre1.cpp"
using namespace std;
int main (void){
vector_t<int>& tabla[10];
tabla[0]= 50;
int buscado=50;
int central=0;
for(int i=0;i<=10;i++){
(tabla[i])++;
}
int izq= 50;
int dch= tabla-1;
central= bsearch(izq,dch,buscado);
}
Thanks
What happens that gives me errors that I do not understand:
In function 'int main()':
6 declaration of 'tabla' as array of references
8 'tabla' was not declared in this scope
22 'template<class T> class vector_t' used without template parameters
In function 'int bsearch(int, int, const T&)':
32 [Error] 'v_' was not declared in this scope