如何传递一个向量到函数? [英] How to pass a vector to a function?

查看:160
本文介绍了如何传递一个向量到函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图发送一个向量作为参数到一个函数,我不知道如何使它的工作。尝试了一堆不同的方式,但他们都给出不同的错误消息。
我只包括部分代码,因为它只有这一部分不工作。
(向量random随机填充,但排序,值介于0和200之间)



更新代码:

  #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>随机(100);

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

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

系统(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);
}


解决方案

向量 c>作为引用或作为指针传递(我忽略了通过值传递它的选项,这显然是不希望的)。



作为参考:

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

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

作为指针:

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

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

内部 binarySearch 。或 - >




  1. > binarySearch 期望一个向量< int> * ,但你传递一个向量< int> ; (在之前缺少之后

  2. 在使用之前,不要取消引用 binarySearch 中的指针(例如, random [mid] (* random)[mid]

  3. 您缺少 using namespace std; < include> s

  4. 您分配给的第一个值最后是错误的(应为0和99而不是 random [0] random [99]


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)

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

解决方案

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

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 random correspondingly.

Issues with your current code

  1. binarySearch expects a vector<int>*, but you pass in a vector<int> (missing a & before random)
  2. You do not dereference the pointer inside binarySearch before 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 first and last are wrong (should be 0 and 99 instead of random[0] and random[99]

这篇关于如何传递一个向量到函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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