The following code will not alter the contents of i
in the for loop:
class Solution {
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
if (target == 0) {return vector<vector<int>>{{}};}
else if (!candidates.size() || target < 0) {return vector<vector<int>>();}
else {
vector<vector<int>> with = combinationSum(candidates, target - candidates[0]);
vector<int> new_vector(candidates.begin() + 1, candidates.end());
vector<vector<int>> without = combinationSum(new_vector, target);
for (auto i : with) {i.push_back(candidates[0]);}
with.insert(with.end(), without.begin(), without.end());
return with;
}
}
};
However, if I change it to auto& i : with ...
, it works. Is there a reason why? I thought references were only relevant if you are working with pointed objects or if you don't want the variable (in this case i
) to change in the local scope.