c++ 函数之间 传递向量
Passing an argument can be of two types generally:
传递参数通常可以分为两种类型:
Pass by value
传递价值
Pass by reference
通过参考
In the case of passing a vector as a parameter in any function of C++, the things are not different. We can pass a vector either by value or by reference.
在C ++的任何函数中将向量作为参数传递的情况下,情况没有什么不同。 我们可以通过值或引用来传递向量。
In the following section, we will discuss when we should use what kind of passing and what's difference b/w them.
在下一节中,我们将讨论何时应该使用哪种传递方式以及它们之间的区别。
1)通过值传递向量 (1) Passing a vector by value)
Below is an example of passing the vector by value. In the below example, we have tried to figure out that if we pass a vector by value, whether local changes in the function reflects the original vector in main or not.
下面是通过值传递向量的示例。 在下面的示例中,我们试图弄清楚,如果我们按值传递向量,则函数中的局部更改是否会反映main中的原始向量。
Let's see the example first, then we will detail what happened and why.
首先让我们看一下示例,然后我们将详细说明发生了什么以及为什么。
#include <bits/stdc++.h>
using namespace std;
void print(vector<int> arr)
{ //paseed by value
//printing the vector which has been passed
cout << "In the print function...\n";
cout << "Printing the array that has been passed by value\n";
for (auto i : arr)
cout << i << " ";
cout << endl;
cout << "chan