我正在尝试将向量作为函数的参数发送,但我不知道如何使它起作用。尝试了多种方法,但是它们都给出了不同的错误消息。我只包括部分代码,因为只有这部分不起作用。(向量“随机”填充有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> 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);
}