C++ STL(19):Sorted Range上的查找算法

#include <iostream>
#include <algorithm>

//Sorted Range上的查找算法
int main()
{
	/************************************************************************/
	//binary_search
	/************************************************************************/
	//binary_search:二分查找,在已排序的[first,last)中寻找元素value,如果存在返回true,否则返回false
	/*
		template<class ForwardIterator, class Type>
		bool binary_search(
			ForwardIterator _First,
			ForwardIterator _Last,
			const Type& _Val
		);

		template<class ForwardIterator, class Type, class BinaryPredicate>
		bool binary_search(
			ForwardIterator _First,
			ForwardIterator _Last,
			const Type& _Val,
			BinaryPredicate _Comp
		);
	*/
	int a[] = { 1, 2, 3, 3, 5, 8 };
	int n = sizeof a / sizeof a[0];
	/*运行结果
		Element 0:not present
		Element 1:present
		Element 2:present
		Element 3:present
		Element 4:not present
		Element 5:present
		Element 6:not present
		Element 7:not present
		Element 8:present
		Element 9:not present
	*/
	for (auto i = 0; i < 10; ++i)
	{
		std::cout << "Element " << i << ":" <<
			(std::binary_search(std::begin(a), std::end(a), i) ? "present" : "not present")
			<< std::endl;
	}

	std::cout << "===================================" << std::endl;
	/************************************************************************/
	//lower_bound
	/************************************************************************/
	/*lower_bound:试图在已排序的[first,last)中寻找元素value
	如果存在value,则函数返回找到的第一个value元素的位置
	如果不存在value,则返回应该出现的位置,即第一个不小于value元素的位置,或者说第一个可安插value的位置
	*/
	/*
		template<class ForwardIterator, class Type>
		ForwardIterator lower_bound(
			ForwardIterator _First,
			ForwardIterator _Last,
			const Type& _Val
		);

		template<class ForwardIterator, class Type, class BinaryPredicate>
		ForwardIterator lower_bound(
			ForwardIterator _First,
			ForwardIterator _Last,
			const Type& _Val,
			BinaryPredicate _Comp
		);
	*/

	/*运行结果
		Searching for 0. Result: index = 0, a[0] == 1
		Searching for 1. Result: index = 0, a[0] == 1
		Searching for 2. Result: index = 1, a[1] == 2
		Searching for 3. Result: index = 2, a[2] == 3
		Searching for 4. Result: index = 4, a[4] == 5
		Searching for 5. Result: index = 4, a[4] == 5
		Searching for 6. Result: index = 5, a[5] == 8
		Searching for 7. Result: index = 5, a[5] == 8
		Searching for 8. Result: index = 5, a[5] == 8
		Searching for 9. Result: index = 6, which is off-the-end.
	*/
	for (auto i = 0; i < 10; ++i)
	{
		int *p = std::lower_bound(std::begin(a), std::end(a), i);
		std::cout << "Searching for " << i << ". ";
		std::cout << "Result: index = " << p - a << ", ";
		if (p != std::end(a))
			std::cout << "a[" << p - a << "] == " << *p << std::endl;
		else
			std::cout << "which is off-the-end." << std::endl;
	}

	std::cout << "=======================================" << std::endl;
	/************************************************************************/
	//upper_bound
	/************************************************************************/
	/*upper_bound:试图在已排序的[first,last)中寻找元素value
	函数返回可被安插的最后一个合适的位置,所以value若存在,则返回的iterator将指向value的下一个位置
	*/
	/*
		template<class ForwardIterator, class Type>
		ForwardIterator upper_bound(
			ForwardIterator _First,
			ForwardIterator _Last,
			const Type& _Val
		);

		template<class ForwardIterator, class Type, class Predicate>
		ForwardIterator upper_bound(
			ForwardIterator _First,
			ForwardIterator _Last,
			const Type& _Val,
			Predicate _Comp
		);
	*/
	int *p = std::upper_bound(std::begin(a), std::end(a), 3);
	std::cout << "a[" << p - a << "] = " << *p << std::endl;//a[4] = 5

	/*运行结果
		Searching for 0. Result: index = 0, a[0] == 1
		Searching for 1. Result: index = 1, a[1] == 2
		Searching for 2. Result: index = 2, a[2] == 3
		Searching for 3. Result: index = 4, a[4] == 5
		Searching for 4. Result: index = 4, a[4] == 5
		Searching for 5. Result: index = 5, a[5] == 8
		Searching for 6. Result: index = 5, a[5] == 8
		Searching for 7. Result: index = 5, a[5] == 8
		Searching for 8. Result: index = 6, which is off-the-end.
		Searching for 9. Result: index = 6, which is off-the-end.
	*/
	for (auto i = 0; i < 10; ++i)
	{
		int *p = std::upper_bound(std::begin(a), std::end(a), i);
		std::cout << "Searching for " << i << ". ";
		std::cout << "Result: index = " << p - a << ", ";
		if (p != std::end(a))
			std::cout << "a[" << p - a << "] == " << *p << std::endl;
		else
			std::cout << "which is off-the-end." << std::endl;
	}

	std::cout << "===================================" << std::endl;
	/************************************************************************/
	//equal_range
	/************************************************************************/
	/*equal_range:lower_bound与upper_bound的结合,
	函数返回一对iterator i和j,i表示可安插的第一个位置,j表示可安插的最后一个位置,
	*/
	/*
		template<class ForwardIterator, class Type>
		pair<ForwardIterator, ForwardIterator> equal_range(
			ForwardIterator _First,
			ForwardIterator _Last,
			const Type& _Val
		);

		template<class ForwardIterator, class Type, class Predicate>
		pair<ForwardIterator, ForwardIterator> equal_range(
			ForwardIterator _First,
			ForwardIterator _Last,
			const Type& _Val,
			Predicate _Comp
		);
	*/
	/*运行结果
		Searching for 2
		First position where 2 could be inserted: 1
		Last position where 2 could be inserted: 2
		*result.first = 2
		*result.second = 3

		Searching for 3
		First position where 3 could be inserted: 2
		Last position where 3 could be inserted: 4
		*result.first = 3
		*result.second = 5

		Searching for 4
		First position where 4 could be inserted: 4
		Last position where 4 could be inserted: 4
		*result.first = 5
		*result.second = 5

		Searching for 5
		First position where 5 could be inserted: 4
		Last position where 5 could be inserted: 5
		*result.first = 5
		*result.second = 8
	*/
	for (auto i = 2; i <= 5; ++i)
	{
		std::pair<int*, int*> result = std::equal_range(std::begin(a), std::end(a), i);
		std::cout << std::endl;
		std::cout << "Searching for " << i << std::endl;
		std::cout << "First position where " << i << " could be inserted: "	<< result.first - a << std::endl;
		std::cout << "Last position where " << i << " could be inserted: "	<< result.second - a << std::endl;

		if (result.first < std::end(a))
			std::cout << "*result.first = " << *result.first << std::endl;
		if (result.second < std::end(a))
			std::cout << "*result.second = " << *result.second << std::endl;
	}
	return 0;
}

====================打个广告,欢迎关注====================

QQ:412425870
csdn博客:
http://blog.youkuaiyun.com/caychen
码云:
https://gitee.com/caychen/
github:
https://github.com/caychen

点击群号或者扫描二维码即可加入QQ群:

328243383(1群)



点击群号或者扫描二维码即可加入QQ群:

180479701(2群)



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值