I have a recursive function where I have an vector of object being passed. I need to traverse this vector and take out elements based on some condition. Which is a more efficient way of doing this -
a) Declaring a temporary vector in the body of the function
function(vector<obj> &arr,int l,int r){
some other stuff
vector<obj> temp;
adding elements based on some condition and operating
}
b) Declaring something like a global temporary vector (or passing it through the functions by reference), and then clearing it inside the function body and performing the required steps.
function(vector<obj> &arr,vector<obj> &temp,int l,int r){
some other stuff
temp.clear();
adding elements based on some condition and operating
}
I agree it might not cause significant improvement in performance, but just want to understand which is a better practice. Do include some other methods if you think it is more efficient.