using namespace std;
int all_occ(int *arr , int key , int n , vector pos){
if(n==0){
return -1;
}
int p = all_occ(arr+1 , key , n-1 , pos);
for(size_t i = 0 ; i<pos.size() ; i++){
pos.at(i)++;
}
if(arr[0] == key){
pos.push_back(0);
return 0;
}
if(p==-1){
return -1;
}
return 0;
}
int main(){
int arr[] = {1,2,3,4,7,8,7,11,12,7};
int key = 7;
// cin>>key;
vector<int> pos ;
pos.reserve(10);
all_occ(arr,key, sizeof(arr)/sizeof(arr[0]) , pos);
for(int i : pos){
cout<<i <<endl;
}
}
above is my code for finding max occurrence in an array, but when this function is returning an empty vector.
but if I try declaring vector as global this is working fine.
why is vector pos not able to maintain itself when returning from the function.