先看一段代码:
vector> allrot(const vector & a) { vector > 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& a; vector a;
参考:stackoverflow-Cpp-Vector
带&表示传入函数的是vector的引用(即物理位置),函数内部对vector改动,vector就会改变;
不带&表示传入的是vector的复制品(开辟了另一块位置),函数内部对其改动,不会影响原本的vector;
原英文解释如下:
When you pass vector
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& then function gets the reference which means that any changes in the function would modify the original data.
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)