向量是按值传递还是按C ++中的引用传递给函数 [英] Are vectors passed to functions by value or by reference in C++

查看:112
本文介绍了向量是按值传递还是按C ++中的引用传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

如果我不得不猜测,我会说你来自Java背景.这是C ++,除非您使用&运算符另行指定,否则事情将按值传递(请注意,此运算符还用作"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

您还可以选择将指针传递给向量(void foo(vector<int> *bar)),但是除非您知道自己在做什么,否则您会觉得这确实是可行的方法,否则请不要这样做.

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

我建议您大致阅读一些有关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);
}

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.

解决方案

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:

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.

Also, vectors are not the 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.

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:

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);
}

这篇关于向量是按值传递还是按C ++中的引用传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆