C++:lower_bound 和 upper_bound

天涯浪子 提交于 2020-03-07 20:56:02

C++:lower_bound 和 upper_bound

1. set , map, multiset, multimap

原型:
iterator lower_bound (const value_type& val) const;

iterator upper_bound (const value_type& val) const;

pair<iterator,iterator> equal_range (const value_type& val) const;

lower_bound返回的是第一个 >= val 的迭代器

upper_bound返回的是第一个 > val 的迭代器

equal_range 返回的是lower_bound、upper_bound构成的上下限的迭代器区间

实例:
struct Y{
	int y, id;
	bool operator < (Y tmp)const// 方式1
	{//注意const  第一个小于第二个
		return y < tmp.y;
	}
}; 
set<Y> s;
set<Y>::iterator it; 
it = s.upper_bound(m);//此处的查找是按照Y中的实例比较的 二分查找

2. 二分查找

头文件:algorithm --原型:
template<typename _ForwardIterator, typename _Tp, typename _Compare>
    bool binary_search(_ForwardIterator __first, _ForwardIterator __last,
        const _Tp&__val, _Compare __comp);

template<typename _ForwardIterator, typename _Tp, typename _Compare>
    inline _ForwardIterator lower_bound(_ForwardIterator __first, _ForwardIterator _last,
		const _Tp& __val, _Compare __comp);

template<typename _ForwardIterator, typename _Tp, typename _Compare>
    inline _ForwardIterator upper_bound(_ForwardIterator __first, _ForwardIterator _last,
		const _Tp& __val, _Compare __comp);

查找的地址处于 first 和 last 之间,前提要保证该地址空间里面的元素是有序的。(默认非降序 <= 的顺序), 第三个参数是查找的对象,第四个参数是 compare 函数。

实例1:无compare / greater<int>()
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
	int a[14] = {1,1,2,2,2,4,5,5,6,8,8,8,10,15};
	
    bool tag = binary_search(a, a+14, 5);
    cout << tag << endl;
    
	int * pos = lower_bound(a, a+ 14, 3); 	//第一个 >= 3 的位置 
	int idx = distance(a, pos); 			// pos-a	
	cout << "a[" << idx << "] = " << *pos << endl;
	
	pos = upper_bound(a, a+14, 6);			// 第一个 > 6 的位置 
	idx = distance(a, pos);
	cout << "a[" << idx << "] = " << *pos << endl;
	
	pos = lower_bound(a, a+14, 7, greater<int>());//数组中第一个 <= 被查数的值 
	cout << *pos << endl;
	
	
	pos = upper_bound(a, a+14, 7, greater<int>());//数组中第一个 <  被查数的值
	cout << *pos << endl;
	
	return 0;
}
实例2:有compare
#include <iostream>
#include <algorithm>
using namespace std;

struct P{
	int x, y;
	P(){x = y = 0;}
	P(int xx, int yy)
	{
		x = xx;
		y = yy;
	}
};
bool cmp(P a, P b)
{
	return a.x < b.x;
}

int main()
{
	P a[5];
	a[0] = P(1, 100);
	a[1] = P(50, 50);
	a[2] = P(24, 68);
	a[3] = P(45, 28);
	a[4] = P(36, 98);	
	sort(a, a+5, cmp);
	
	cout << lower_bound(a, a+5, P(50, 1000), cmp) - a << endl;
	// 第一个x值>=50 的元素是(50, 50),它的下标为4
	
	return 0;
}

3.很久之前写的二分查找

#include <cstdio>
int Find(int *a, int n, int k);

int main()
{
	int a[10024], n, i, j, k;
	scanf("%d", &n);
	for (i = 0; i < n; ++i)
	{
		scanf("%d", a + i);
	}
	scanf("%d", &k);
	j = Find(a, n, k);
	 
	if (!j) printf("NOT FOUND!\n");
	else    printf("%d\n", j);
}

int Find(int *a, int n, int k)
{
	int i= 0, j = n- 1, r, p = 0;
	while (i <= j && !p)
	{
		r = (i + j) / 2; 
		if (k < a[r])      j = r - 1;
		else if (k > a[r]) i = r + 1;
		else               p = r + 1;
	}
	return p;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!