[英]what's the difference between "vector<int>& a " and "vector<int> a"?
I'm the beginner in C++.我是 C++ 的初学者。 I try to write a program to rotate a vector one by one
我尝试编写一个程序来逐个旋转向量
ie, {1,2,3,4,5} --> {2,3,4,5,1} --> {3,4,5,1,2}即,{1,2,3,4,5} --> {2,3,4,5,1} --> {3,4,5,1,2}
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;
}
This doesn't work, and I have several questions.这不起作用,我有几个问题。
vector<int>& a
rather than vector<int> a
?vector<int>& a
而不是vector<int> a
? Thank you for your help谢谢您的帮助
When you pass vector<int>
then function gets a copy of that vector.当您传递
vector<int>
时,函数会获取该向量的副本。 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.当您传递
vector<int>&
然后函数获取引用,这意味着函数中的任何更改都会修改原始数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.