3
I wanted to do a recursive linear search using vector and iterators, here’s the code:
long int busca_sr(std::vector<long int> &amostra, const long int &key){
auto first = amostra.begin();
auto last = amostra.end();
if (last == first-1){
return -1;
}
else if(*last == key){
return 1;
}
else{
--last;
std::vector <long int> auxi1 (first, last);
return busca_sr(auxi1, key);
}
The problem is that when I run this function my pc hangs, I suspect that the error is in the stopping condition of my recursion, because when the last is equal to the first the auxiliary vector down there will not be allocated, wanted a way to insert a stop condition without having to change the function signature!
You should not dereference the value of end(), which occurs in *last == key. source: http://www.cplusplus.com/reference/vector/vector/end/
– Anderson F. Viana