直接看代码:
#include <iostream>
#include <string>
#include <vector>
#include <ctype.h>
using namespace std;
int main()
{
vector<int> v1;
vector<int>::iterator start;
vector<int>::iterator end;
vector<int>::iterator mid;
int loop = 0;
int key = 11;
for (loop = 0; loop < 10; ++loop)
{
v1.push_back(loop);
}
start = v1.begin();
end = v1.end();
while(start < end)
{
mid = start + (end - start)/2;
if(*mid == key)
{
cout<< mid - v1.begin();
return 0;
}
else if(*mid > key)
{
end = mid;
}
else
{
start = mid + 1;
}
}
cout << "can not find the matched value"<<endl;
return -1;
}
转载请注明原文地址: https://ju.6miu.com/read-675990.html