先看一段代码:
vector<vector<int>> allrot(const vector<int>& a)
{
vector<vector<int>> result;
for (int i = 0; i < a.size(); i ++ ){
rotate(a.begin(), a.begin() + 1, a.end());
result.push_back(a);
}
return result;
}
当vector当作形参输入到函数时,有两种方法:
vector<int>& a;
vector<int> a;
参考:stackoverflow-Cpp-Vector
带&表示传入函数的是vector的引用(即物理位置),函数内部对vector改动,vector就会改变;
不带&表示传入的是vector的复制品(开辟了另一块位置),函数内部对其改动,不会影响原本的vector;
原英文解释如下:
When you pass vector<int> then function gets a copy of that vector. You can do anything you want with it in the function and your original data would not change.
When you pass vector<int>& then function gets the reference which means that any changes in the function would modify the original data.