C++ 如何将向量传递给函数?

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

How to pass a vector to a function?

c++functionvector

提问by Joe

I'm trying to send a vector as an argument to a function and i can't figure out how to make it work. Tried a bunch of different ways but they all give different error messages. I only include part of the code, since it's only this part that doesn't work. (the vector "random" is filled with random, but sorted, values between 0 and 200)

我正在尝试将向量作为参数发送给函数,但我不知道如何使其工作。尝试了很多不同的方法,但它们都给出了不同的错误消息。我只包含部分代码,因为只有这部分不起作用。(向量“随机”填充了随机但已排序的 0 到 200 之间的值)

Updated the code:

更新了代码:

#include <iostream>     
#include <ctime>        
#include <algorithm>    
#include <vector>       

using namespace std;

int binarySearch(int first, int last, int search4, vector<int>& random);

int main()
{
    vector<int> random(100);

    int search4, found;
    int first = 0;
    int last = 99;

    found = binarySearch(first, last, search4, random);

    system("pause");    
    return(0);      
}

int binarySearch(int first, int last, int search4, vector<int>& random)
{
    do
    {
        int mid = (first + last) / 2;  
        if (search4 > random[mid]) 
            first = mid + 1;  
        else if (search4 < random[mid]) 
            last = mid - 1; 
        else
            return mid;     
    } while (first <= last); 

    return -(first + 1);
}

回答by Jon

It depends on if you want to pass the vectoras a reference or as a pointer (I am disregarding the option of passing it by value as clearly undesirable).

这取决于您是想将 传递vector为引用还是作为指针(我无视按值传递它的选项显然是不可取的)。

As a reference:

作为参考:

int binarySearch(int first, int last, int search4, vector<int>& random);

vector<int> random(100);
// ...
found = binarySearch(first, last, search4, random);

As a pointer:

作为指针:

int binarySearch(int first, int last, int search4, vector<int>* random);

vector<int> random(100);
// ...
found = binarySearch(first, last, search4, &random);

Inside binarySearch, you will need to use .or ->to access the members of randomcorrespondingly.

在 内binarySearch,您将需要使用.或相应->地访问 的成员random

Issues with your current code

您当前代码的问题

  1. binarySearchexpects a vector<int>*, but you pass in a vector<int>(missing a &before random)
  2. You do not dereference the pointer inside binarySearchbefore using it (for example, random[mid]should be (*random)[mid]
  3. You are missing using namespace std;after the <include>s
  4. The values you assign to firstand lastare wrong (should be 0 and 99 instead of random[0]and random[99]
  1. binarySearch期望 a vector<int>*,但您传入 a vector<int>&之前缺少 a random
  2. binarySearch在使用它之前不要取消引用里面的指针(例如,random[mid]应该是(*random)[mid]
  3. using namespace std;<include>s之后失踪了
  4. 分配给值firstlast是错误的(应该在0到99,而不是random[0]random[99]

回答by Mario

You'll have to pass the pointer to the vector, not the vector itself. Note the additional '&' here:

您必须将指针传递给向量,而不是向量本身。注意这里额外的“&”:

found = binarySearch(first, last, search4, &random);

回答by Jerry Coffin

Anytime you're tempted to pass a collection (or pointer or reference to one) to a function, ask yourself whether you couldn't pass a couple of iterators instead. Chances are that by doing so, you'll make your function more versatile (e.g., make it trivial to work with data in another type of container when/if needed).

每当您想将一个集合(或指向一个集合的指针或引用)传递给一个函数时,问问自己是否不能传递几个迭代器。这样做很有可能使您的功能更加通用(例如,在需要时/如果需要,可以轻松处理另一种类型容器中的数据)。

In this case, of course, there's not much point since the standard library already has perfectly good binary searching, but when/if you write something that's not already there, being able to use it on different types of containers is often quite handy.

在这种情况下,当然,没有多大意义,因为标准库已经具有非常好的二进制搜索,但是当/如果您编写一些不存在的东西时,能够在不同类型的容器上使用它通常非常方便。

回答by corsiKa

You're passing in a pointer *randombut you're using it like a reference &random

您正在传递一个指针,*random但您正在像引用一样使用它&random

The pointer (what you have) says "This is the address in memory that contains the address of random"

指针(你所拥有的)说“这是内存中包含随机地址的地址”

The reference says "This is the address of random"

参考文献说“这是随机地址”

回答by Daniel A. White

found = binarySearch(first, last, search4, &random);

Notice the &.

请注意&.

回答by Puppy

You're using the argument as a reference but actually it's a pointer. Change vector<int>*to vector<int>&. And you should really set search4to something before using it.

您将参数用作参考,但实际上它是一个指针。更改vector<int>*vector<int>&search4在使用它之前,你真的应该设置一些东西。

回答by Javad Yousefi

If you use randominstead of * randomyour code not give any error

如果你使用random而不是* random你的代码不会给出任何错误