Reputation: 31
I'm the beginner in C++. I try to write a program to rotate a vector one by one
i.e., {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
?Thank you for your help
Upvotes: 1
Views: 9006
Reputation: 1571
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.
Upvotes: 4