#include <bits/stdc++.h>
#include <vector>
using namespace std;
int search(vector<int>& num, int target){
int left = 0, right = num.size() - 1;
while(left <= right){
int m = (left+right)/2;
if(num[m] == target) return m;
else if(num[m] > target) right = m-1;
else left = m + 1;
}
return -1;
}
int main()
{
int a[6] = {-1, 0, 3, 5, 9, 12};
vector<int> num(a, a+6);
cout << search(num, 9);
return 0;
}
二分查找C++实现
猜你喜欢
转载自blog.csdn.net/zdx1996/article/details/86562584
今日推荐
周排行