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

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

问题描述

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

解决方案

在 C++ 中,除非您使用 &-operator 另行指定,否则事物是按值传递的(请注意,此运算符也用于作为address-of"运算符,但在不同的上下文中).这一切都有据可查,但我还是要重申一遍:

void foo(vector bar);//按值void foo(vector &bar);//通过引用(非常量,所以可以在 foo 内修改)void foo(vector const &bar);//通过常量引用

你也可以选择传递一个指向向量的指针(void foo(vector *bar)),但除非你知道你在做什么并且你觉得这真的是这是要走的路,不要这样做.

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

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

void foo1(int *arr) { cout <<大小(arr)<<'\n';}void foo2(int arr[]) { cout <<大小(arr)<<'\n';}void foo3(int arr[10]) { cout <<大小(arr)<<'\n';}void foo4(int (&arr)[10]) { cout <<大小(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.

解决方案

In C++, 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天全站免登陆