简体   繁体   English

&quot;矢量&quot; 和有什么不一样<int> &amp; 一个 &quot; 和 &quot; 向量<int>一个”?

[英]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.这不起作用,我有几个问题。

  1. Why should I use vector<int>& a rather than vector<int> a ?为什么我应该使用vector<int>& a而不是vector<int> a
  2. What's wrong with my code ?我的代码有什么问题?

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.

相关问题 &quot;vector&quot; 和有什么不一样<int> v[]”和“向量<vector<int> &gt; v&quot; - What is the difference between "vector<int> v[]" and "vector<vector<int>> v" 对于C++向量初始化,“向量”有什么区别<int> v = n;”和“向量<int> v(n);"</int></int> - For C++ vector initialization, what's the difference between "vector<int>v = n;" and "vector<int>v(n);" std :: vector和有什么不一样 <int> 和std :: vector <int> *? - What is the difference between std::vector<int> and std::vector<int>*? “auto x = vector <int>()”和“vector <int> x”之间有什么区别? - What's the difference between “auto x = vector<int>()” and “vector<int> x”? `vector <int> v;`和`vector <int> v = vector <int>();`之间的区别 - Difference between `vector<int> v;` and `vector<int> v = vector<int>();` 向量和向量有什么区别<int>一、向量<int> a[n] 和向量<int>一个)?</int></int></int> - What is the difference between vector<int> a , vector<int> a[n] and vector<int> a(n)? 向量之间的差异<int> V[] 和向量&lt;向量<int> &gt; 五</int></int> - Difference between vector <int> V[] and vector< vector<int> > V 向量和向量有什么区别<vector<int> &gt; v 和向量<int> * v 在 memory 配置? </int></vector<int> - What is the difference between vector<vector<int> > v and vector <int>* v in memory allocation? 向量之间有什么区别 <int> ()与向量 <int> {} vs NULL vs size = 0? - What's the difference among vector<int>() vs vector<int>{} vs NULL vs size=0? vector的时间复杂度有什么区别<int> a {N, 0} 和 int arr a[N] = {0} - What is the difference between the time complexities of vector <int> a {N, 0} and int arr a[N] = {0}
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM