#include <QCoreApplication>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
/*采用二分法查找数组的目标元素自己*/
int search(const vector<int>& num,int target){
int first=0,last=num.size();
while(first!=last){
const int mid=(first+last)/2;
if(num[mid]==target){
return mid;
}
if(num[first]<=num[mid]){
if(num[first]<=target&&target<num[mid])
last=mid;
else
first=mid+1;
} else {
if(num[mid]<target&&target<=num[last-1])
first=mid+1;
else
last=mid;
}
}
return -1;
}
void display(const int &data){
cout<<data<<' ';
}
int main()
{
int a[]={1,2,3,5};
vector<int> vec(a,a+sizeof(a)/sizeof(int));
for_each(vec.begin(),vec.end(),display);
cout<<endl;
int ret=search(vec,5);
cout<<ret;
return 1;
}