将向量传递给函数,值与引用 C++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26647152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:39:40  来源:igfitidea点击:

passing vectors to a function, value vs reference C++

c++arraysvector

提问by Adam

I'm coding in C++. If I have some function void foo(vector<int> test)and I call it in my program, will the vector be passed by value or reference? I'm unsure because I know vectors and arrays are similar and that a function like void bar(int test[])would pass test in by reference (pointer?) instead of by value. My guess is that I would need to pass the vector by pointer/reference explicitly if I wanted to avoid passing by value but I'm not sure.

我正在用 C++ 编码。如果我有一些函数void foo(vector<int> test)并在我的程序中调用它,向量是通过值还是引用传递?我不确定,因为我知道向量和数组是相似的,并且像这样的函数void bar(int test[])会通过引用(指针?)而不是通过值传递测试。我的猜测是,如果我想避免按值传递,我需要通过指针/引用显式传递向量,但我不确定。

回答by JorenHeit

If I had to guess, I'd say that you're from a Java background. This is C++, and things are passed by value unless you specify otherwise using the &-operator (note that this operator is also used as the 'address-of' operator, but in a different context). This is all well documented, but I'll re-iterate anyway:

如果我不得不猜测,我会说你有 Java 背景。这是 C++,除非您使用&-operator另行指定,否则事物按值传递(请注意,此运算符也用作“address-of”运算符,但在不同的上下文中)。这都是有据可查的,但无论如何我都会重申:

void foo(vector<int> bar); // by value
void foo(vector<int> &bar); // by reference (non-const, so modifiable inside foo)
void foo(vector<int> const &bar); // by const-reference

You can also choose to pass a pointer to a vector (void foo(vector<int> *bar)), but unless you know what you're doing and you feel that this is really is the way to go, don't do this.

您也可以选择传递一个指向向量 ( void foo(vector<int> *bar))的指针,但除非您知道自己在做什么并且您觉得这确实是要走的路,否则不要这样做。

Also, vectors are notthe same as arrays! Internally, the vector keeps track of an array of which it handles the memory management for you, but so do many other STL containers. You can't pass a vector to a function expecting a pointer or array or vice versa (you can get access to (pointer to) the underlying array and use this though). Vectors are classes offering a lot of functionality through its member-functions, whereas pointers and arrays are built-in types. Also, vectors are dynamically allocated (which means that the size may be determined and changed at runtime) whereas the C-style arrays are statically allocated (its size is constant and must be known at compile-time), limiting their use.

此外,载体是一样的阵列!在内部,vector 会跟踪一个数组,它为您处理内存管理,但许多其他 STL 容器也是如此。您不能将向量传递给需要指针或数组的函数,反之亦然(您可以访问(指向)底层数组并使用它)。向量是通过其成员函数提供许多功能的类,而指针和数组是内置类型。此外,向量是动态分配的(这意味着可以在运行时确定和更改大小),而 C 样式数组是静态分配的(其大小是常数,必须在编译时知道),限制了它们的使用。

I suggest you read some more about C++ in general (specifically array decay), and then have a look at the following program which illustrates the difference between arrays and pointers:

我建议你阅读更多关于 C++ 的一般内容(特别是数组衰减),然后看看下面的程序,它说明了数组和指针之间的区别:

void foo1(int *arr) { cout << sizeof(arr) << '\n'; }
void foo2(int arr[]) { cout << sizeof(arr) << '\n'; }
void foo3(int arr[10]) { cout << sizeof(arr) << '\n'; }
void foo4(int (&arr)[10]) { cout << sizeof(arr) << '\n'; }

int main()
{
    int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    foo1(arr);
    foo2(arr);
    foo3(arr);
    foo4(arr);
}

回答by Ajay

A vectoris functionally same as an array. But, to the language vectoris a type, and intis also a type. To a function argument, an array of any type (including vector[]) is treated as pointer. A vector<int>is not same as int[](to the compiler). vector<int>is non-array, non-reference, and non-pointer - it is being passed by value, and hence it will call copy-constructor.

Avector在功能上与数组相同。但是,语言vector是一种类型,int也是一种类型。对于函数参数,任何类型(包括vector[])的数组都被视为指针。Avector<int>int[](对编译器而言)不同。vector<int>是非数组、非引用和非指针——它是按值传递的,因此它将调用复制构造函数。

So, you must use vector<int>&(preferably with const, if function isn't modifying it) to pass it as a reference.

因此,您必须使用vector<int>&(最好使用const,如果函数未修改它)将其作为引用传递。

回答by ravi

void foo(vector<int> test)

void foo(vector<int> test)

vector would be passed by value in this.

向量将在此按值传递。

You have more ways to pass vectors depending on the context:-

根据上下文,您有更多传递向量的方法:-

1) Pass by reference:- This will let function foo change your contents of the vector. More efficient than pass by value as copying of vector is avoided.

1) 通过引用传递:- 这将使函数 foo 更改您的向量内容。由于避免了向量的复制,因此比按值传递更有效。

2) Pass by const-reference:- This is efficient as well as reliable when you don't want function to change the contents of the vector.

2)通过const-reference传递:-当您不希望函数更改向量的内容时,这既有效又可靠。

回答by Nishant Yadav

when we pass vector by value in a function as an argument,it simply creates the copy of vector and no any effect happens on the vector which is defined in main function when we call that particular function. while when we pass vector by reference whatever is written in that particular function, every action will going to perform on the vector which is defined in main or other function when we call that particular function.

当我们在函数中按值传递向量作为参数时,它只是创建向量的副本,当我们调用该特定函数时,它不会对主函数中定义的向量产生任何影响。而当我们通过引用传递该特定函数中写入的任何内容时,当我们调用该特定函数时,每个动作都将在 main 或其他函数中定义的向量上执行。