//#include
//#include
//#include
#include
using std::string;
#include
#include
#include
#include
using std::vector;
bool search_value(std::vector <int>::const_iterator* start,std::vector <int>::const_iterator* end,std::vector <int> ::size_type size,const int search_values,std::vector <int>::const_iterator* &results);
bool search_value(std::vector <int>::const_iterator* start,std::vector <int>::const_iterator* end,std::vector <int> ::size_type size,const int search_values,std::vector <int>::const_iterator* &results){
std::vector <int>::const_iterator mid=(*start)+int(ceil(size/2))-1;
//return true;
//std::cout<<*mid<
if(*mid==search_values){
results=∣
return true;
}
if(*midint size=(*end)-mid+1;
return search_value(&mid,end,size,search_values,results);
}
if(*mid>search_values){
int size=mid-(*start)+1;
return search_value(start,&mid,size,search_values,results);
}
return false;
}
int main()
{
//指针千万注意初始化,否则有崩溃的风险
std::vector <int>::const_iterator* tts = NULL;
bool rest;
//查找元素在容器中的位置
std::vector <int>::difference_type position;
vector <int> ivec={1,2,3,4,18,23,85,22};
vector <int>::const_iterator start=ivec.begin();
vector<int>::const_iterator end=ivec.end();
int aa=23;
int size=ivec.size();
rest=search_value(&start,&end,size,aa,tts);
if(NULL == tts) {
std::cout << "tts is NULL." << std::endl;
}
if(rest){
position=*tts-start+1;
std::cout<<"the position in container is:"<",the value of the search is:"<<*(*tts)<<std::endl;
}else{
std::cout<<"no values match"<<std::endl;
}
}
结果如下: