im pretty new to C++, learning by doing right now. Within the class material, I have the following function:
std::vector<std::complex<double>> material::get_idx(const std::vector<double>& numbers) {
std::vector<std::complex<double>> res;
for (auto& num : numbers) {
// calculate with num (we don't modify it) and push_back to res
...
}
return res;
}
A couple of minutes ago I used the normal for loop like this: for(size_t i=0; i<numbers.size(); i++)
. Since I've read that using ranged-based for loop with auto can be more efficient than plain for loops, I changed it. Now I'm asking myself if it's correct/senseful/good practice to write the ranged-based for loop with a reference like this: for(auto& num : numbers)
and what's the difference to using for(auto num : numbers)
. I'm kind of confused because I'm already handing over the numbers vector per reference in the signature of the get_idx function. Of course I want to hand over the vector numbers to the function get_idx per reference, but how about looping over those elements via reference?