将输入参数直接输入到vector ,建议随时参考​​cpp参考网址​​ 基本上多少都会忘记一些,多看几遍就记住了

#include<iostream>
#include<vector>
#include<iterator>
using namespace std;

int binary(vector<int>& nums, int target) {
int left = 0, right = nums.size();

while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] == target) {
return mid;
} else if (nums[mid] > target) { // 偏大,缩小右区间
right = mid;
return mid;
} else if (nums[mid] < target) { // 偏小,缩小左区间
left = mid + 1;
}
}
return -1;
}

int main()
{
istream_iterator<int> in(cin); // 输入迭代器
istream_iterator<int> end; // 无参默认为迭代器边界end()
vector<int> nums;

nums.assign(in, end);
cout <<binary(nums, 5) <<endl;
return 0;
}