12.2 STL二分查找算法

#include<iostream>
#include<algorithm>
using namespace std;

struct rule{
	bool operator()(const int &a1, const int &a2)
	{
		return a1%10 < a2%10;//个位数从小到大排 
	}
};

void print(int a[],int n)
{
	for(int i = 0; i < n; ++i)
		cout << a[i] << " ";
	cout << endl; 
}

int main()
{
	int a[]={12,45,3,98,21,7};
	sort(a,a+6);//从小到大
	print(a,6);//3,7,12,21,45,98 
	cout << binary_search(a,a+6,12) << endl; //找到了输出1
	cout << binary_search(a,a+6,77) << endl; //找不到输出0
	sort(a,a+6,rule());
	print(a,6);//21,12,3,45,7,98
	cout << binary_search(a,a+6,7) << endl;//0,因为不是从小到大排好序的,规则不同,这样的查找是没有意义的,所以不一定会找到7
	cout << binary_search(a,a+6,8,rule()) << endl;//1,查找规则和数字的排序规则一致,只个位数,所以能找到8 
	return 0; 
}

排序规则要和二分查找的规则一致,否则查找将毫无意义。二分查找的数必须是排好顺序的。

lower_bound:查找>=k的数的最小下标。

upper_bound:查找>k的数的最小下标。

#include<iostream>
#include<algorithm>
#define N 7
using namespace std; 

struct rule{
	bool operator()(const int &a1, const int &a2)
	{
		return a1%10 < a2%10;
	}
};

void print(int a[], int num)
{
	for(int i = 0; i < num; ++ i)
		cout << a[i] << " ";
	cout << endl;
}
//int N = 7;
int main()
{
	int a[N] = {11,3,5,34,5,7,21};
	sort(a,a+N);
	print(a,N);//3 5 5 7 11 21 34
	int *p1 = lower_bound(a,a+N,5);//p1取>=5的最小数的地址 
	cout << *p1 << " " << p1 - a << endl;//5 1
	int *p2 = upper_bound(a,a+N,5);//p2取>5的最小数的地址 
	cout << *p2 << " " << p2 - a << endl;// 7 3
	int *p3 = upper_bound(a,a+N,8);
	cout << *p3 << " " << p3 - a << endl;//11 4
	int *p4 = lower_bound(a,a+N,8);
	cout << *p4 << " " << p4 - a << endl;//11 4
	sort(a,a+N,rule());
	print(a,N);//11 21 3 34 5 5 7
	int *p5 = lower_bound(a,a+N,5,rule());//个位数>=5的最小数的地址 
	cout << *p5 << " " << p5 - a << endl;//5 4
	int *p6 = upper_bound(a,a+N,5,rule()); 
	cout << *p6 << " " << p6 - a << endl;//7 6
	int *p7 = lower_bound(a,a+N,8,rule());//大于最后一位的数都会输出下一个位置 
	cout << *p7 << " " << p7 - a << endl;//0 7
	if(lower_bound(a,a+N,9,rule()) == a + N)
		cout << "not found" << endl;//not found 
	int *p8 = lower_bound(a,a+N,4,rule());
	cout << *p8 << " " << p8 - a << endl;//34 3
	int *p9 = upper_bound(a,a+N,4,rule());
	cout<< *p9 <<  " " << p9 - a << endl;//5 4;
	return 0;
 }
 

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
struct Student{
	char name[20];
	int id;
	double gpa;
}; 

Student a[]={{"Jack",112,3.4},{"Mary",102,3.8},{"Mary",117,3.9},{"Ala",333,3.5},{"Zero",101,4.0}};

struct rule1{
	bool operator()(const Student &a1, const Student &a2)//姓名从小到大 
	{
		return strcmp(a1.name,a2.name)<0;
	}
}; 

struct rule2{
	bool operator()(const Student &a1, const Student &a2)//id从小到大 
	{
		return a1.id < a2.id;
	}
};

struct rule3{
	bool operator()(const Student &a1, const Student &a2)//gpa从大到小 
	{
		return a1.gpa > a2.gpa;
	}
}; 

int main()
{
	Student s;
	strcpy(s.name,"Mary");
	s.id = 102;
	s.gpa = 3.8;
	sort(a,a+5,rule1());
	cout << binary_search(a,a+5,s,rule1()) << endl;//1
	strcpy(s.name,"ttt");
	cout << binary_search(a,a+5,s,rule1()) << endl;//0
	sort(a,a+5,rule2());
	cout << binary_search(a,a+5,s,rule2()) << endl;//1
	s.id = 339;
	cout << binary_search(a,a+5,s,rule2()) << endl;//0
	sort(a,a+5,rule3());
	cout << binary_search(a,a+5,s,rule3()) << endl;//1
	return 0;		
} 

用binary_search进行查找的时候,只用管排序的顺序和排序规则是否一致,比如说对id进行排序后,查找某个id,看看是否能找到,此时只用看查找的这个id是否在按id排好序的序列中即可,不用管name和gpa是否在里面。

猜你喜欢

转载自blog.csdn.net/yanyanwenmeng/article/details/82385935
今日推荐