C++ 通过引用传递向量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15890427/
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-27 19:51:38  来源:igfitidea点击:

Passing vector by reference

c++

提问by user2252786

Using normal C arrays I'd do something like that:

使用普通的 C 数组我会做这样的事情:

void do_something(int el, int **arr)
{
   *arr[0] = el;
   // do something else
}

Now, I want to replace standard array with vector, and achieve the same results here:

现在,我想用向量替换标准数组,并在此处实现相同的结果:

void do_something(int el, std::vector<int> **arr)
{
   *arr.push_front(el); // this is what the function above does
}

But it displays "expression must have class type". How to do this properly?

但它显示“表达式必须具有类类型”。如何正确地做到这一点?

回答by Jon Purdy

You can pass the container by reference in order to modify it in the function. What other answers haven't addressed is that std::vectordoes not have a push_frontmember function. You can use the insert()member function on vectorfor O(n) insertion:

您可以通过引用传递容器,以便在函数中对其进行修改。其他答案没有解决的是std::vector没有push_front成员函数。您可以使用insert()成员函数 onvector进行 O(n) 插入:

void do_something(int el, std::vector<int> &arr){
    arr.insert(arr.begin(), el);
}

Or use std::dequeinstead for amortised O(1) insertion:

std::deque用于摊销 O(1) 插入:

void do_something(int el, std::deque<int> &arr){
    arr.push_front(el);
}

回答by 4pie0

If you define your function to take argument of std::vector<int>& arrand integer value, then you can use push_backinside that function:

如果您定义您的函数以获取参数 std::vector<int>& arr和整数值,那么您可以push_back在该函数内使用 :

void do_something(int el, std::vector<int>& arr)
{
    arr.push_back(el);
    //....
}

usage:

用法:

std::vector<int> arr;
do_something(1, arr); 

回答by taocp

void do_something(int el, std::vector<int> **arr)

should be

应该

void do_something(int el, std::vector<int>& arr)
{
    arr.push_back(el);
}

Pass by reference has been simplified to use the &in C++.

通过引用已被简化为&在 C++ 中使用。

回答by Legat

You can pass vector by referencejust like this:

您可以像这样通过引用传递向量

void do_something(int el, std::vector<int> &arr){
    arr.push_back(el);
}

However, note that this function would always add a new element at the backof the vector, whereas your array functionactually modifies the first element (or initializes it value).

但是,请注意,此函数始终会在向量的后面添加一个新元素,而您的数组函数实际上会修改第一个元素(或初始化它的值)

In order to achieve exactly the same result you should write:

为了获得完全相同的结果,您应该编写:

void do_something(int el, std::vector<int> &arr){
    if (arr.size() == 0) { // can't modify value of non-existent element
        arr.push_back(el);
    } else {
        arr[0] = el;
    }
}

In this way you either add the first element (if the vector is empty) or modify its value (if there first element already exists).

通过这种方式,您可以添加第一个元素(如果向量为空)或修改其值(如果第一个元素已经存在)。

回答by Mppl

You don't need to use **arr, you can either use:

您不需要使用 **arr,您可以使用:

void do_something(int el, std::vector<int> *arr){
    arr->push_back(el);
}

or:

或者:

 void do_something(int el, std::vector<int> &arr){
    arr.push_back(el);
}

**arr makes no sense but if you insist using it, do it this way:

**arr 没有意义,但如果你坚持使用它,请这样做:

void do_something(int el, std::vector<int> **arr){
    (*arr)->push_back(el);
}

but again there is no reason to do so...

但同样没有理由这样做......