先来复习一下最经典的大一就学的binary_search。
#include<iostream>
#include<fstream>
#include<algorithm>
using namespace std;
class MY{
int a;
//public:
friend ostream& operator <<(ostream& os,const MY &my){
os<<my.a;return os;
}
friend istream& operator >>(istream& is,MY& my){
is>>my.a;return is;
}
public:
MY(int a){this->a=a;}
MY(){}
bool operator<(const MY& another){
return this->a<another.a;
}
};
template<class T>
int binary_search(T A[],int n,T key)
{
int low=0,high=n-1;
while(low<=high){
int middle=(low+high)/2;
if(A[middle]<key) low=middle+1;
else if(key<A[middle]) high=middle-1;
else return middle;
}
return -low-1;
}
/*
10
0 1 2 2 2 4 5 6 6 6
*/
int main()
{
ifstream fin("10.txt");
if(!fin) exit(EXIT_FAILURE);
int n;fin>>n;MY* A=new MY[n];
for(int i=0;i<n;i++) fin>>A[i];
cout<<"数组A的内容:";
for(int i=0;i<n;i++) cout<<A[i]<<" ";
cout<<endl;
cout<<"查找"<<endl;
cout<<"-99:"<