STL算法---查找算法(四)

23 篇文章 0 订阅
13 篇文章 0 订阅

9. lower_bound和upper_bound

lower_bound返回一个非递减序列[first, last)中第一个大于等于值val的位置。
upper_bound返回一个非递减序列[first, last)中第一个大于val的位置。

lower_bound
函数原形
template<class FwdIt, class T> FwdIt lower_bound(FwdIt first, FwdIt last, const T& val);
template<class FwdIt, class T, class Pred> FwdIt lower_bound(FwdIt first, FwdIt last, const T& val, Pred pr);

upper_bound
函数原形
template<class FwdIt, class T> FwdIt upper_bound(FwdIt first, FwdIt last, const T& val);
template<class FwdIt, class T, class Pred> FwdIt upper_bound(FwdIt first, FwdIt last, const T& val, Pred pr);
/
#include "stdafx.h"
#include <algorithm>
#include <numeric>
#include <functional>
#include <vector>
#include <iostream>

int _tmain(int argc, _TCHAR* argv[])
{
	std::vector<int> nV;
	std::vector<int>::iterator iter;
	std::vector<int>::iterator iterBegin;
	std::vector<int>::iterator iterEnd;

	// 升序
	nV.clear();
	nV.push_back(4);
	nV.push_back(6);
	nV.push_back(14);
	nV.push_back(14);
	nV.push_back(15);
	nV.push_back(16);
	nV.push_back(18);
	nV.push_back(18);
	nV.push_back(19);

	iterBegin = nV.begin();
	iterEnd = nV.end();

	iter = std::lower_bound(iterBegin, iterEnd, 14);		// *iter输出14
	iter = std::lower_bound(iterBegin, iterEnd, 17);		// *iter输出18

	iter = std::upper_bound(iterBegin, iterEnd, 14);		// *iter输出15
	iter = std::upper_bound(iterBegin, iterEnd, 17);		// *iter输出18
	return 0;
}

10. search

在[first1, last1)中, 查找成功指向第一个范围内第一次出现子序列(第二个范围)的位置,查找失败指向last1,
重载版本使用自定义的比较操作
函数原形
template<class FwdIt1, class FwdIt2> FwdIt1 search(FwdIt1 first1, FwdIt1 last1,FwdIt2 first2, FwdIt2 last2);
template<class FwdIt1, class FwdIt2, class Pred> FwdIt1 search(FwdIt1 first1, FwdIt1 last1, FwdIt2 first2, FwdIt2 last2, Pred pr);
/
#include "stdafx.h"
#include <algorithm>
#include <numeric>
#include <functional>
#include <vector>
#include <iostream>

int _tmain(int argc, _TCHAR* argv[])
{
	std::vector<int> nV;
	std::vector<int>::iterator iter;
	std::vector<int>::iterator iterBegin;
	std::vector<int>::iterator iterEnd;

	nV.clear();
	nV.push_back(11);
	nV.push_back(13);
	nV.push_back(14);
	nV.push_back(14);
	nV.push_back(15);		// 第1组
	nV.push_back(14);		//
	nV.push_back(14);		//
	nV.push_back(16);
	nV.push_back(17);
	nV.push_back(16);
	nV.push_back(15);		// 第2组
	nV.push_back(14);		// 
	nV.push_back(14);		// 
	nV.push_back(18);
	nV.push_back(18);
	nV.push_back(19);

	iterBegin = nV.begin();
	iterEnd = nV.end();

	std::vector<int> nV1;
	nV1.push_back(15);
	nV1.push_back(14);
	nV1.push_back(14);

	iter = std::search(iterBegin, iterEnd, nV1.begin(), nV1.end());	// 返回上面"第1组"第一个元素所在的迭代器

	nV1.clear();
	nV1.push_back(40);
	nV1.push_back(14);
	nV1.push_back(14);
	nV1.push_back(14);
	iter = std::search(iterBegin, iterEnd, nV1.begin(), nV1.end());	// 返回nV.end();

	return 0;
}
说明:

1. find_end查找到最后一组就返回, 而search查找到第一组就返回


11. search_n

在指定范围内查找val出现n次的子序列。
函数原形
template<class FwdIt, class Dist, class T> FwdIt search_n(FwdIt first, FwdIt last,Dist n, const T& val);
template<class FwdIt, class Dist, class T, class Pred> FwdIt search_n(FwdIt first, FwdIt last,Dist n, const T& val, Pred pr);
/
#include "stdafx.h"
#include <algorithm>
#include <numeric>
#include <functional>
#include <vector>
#include <iostream>

int _tmain(int argc, _TCHAR* argv[])
{
	std::vector<int> nV;
	std::vector<int>::iterator iter;
	std::vector<int>::iterator iterBegin;
	std::vector<int>::iterator iterEnd;


	nV.clear();
	nV.push_back(11);
	nV.push_back(13);
	nV.push_back(14);		// A
	nV.push_back(14);
	nV.push_back(15);
	nV.push_back(14);		// B
	nV.push_back(14);
	nV.push_back(16);
	nV.push_back(17);
	nV.push_back(16);
	nV.push_back(15);
	nV.push_back(14);		// C
	nV.push_back(14);
	nV.push_back(18);
	nV.push_back(18);
	nV.push_back(19);

	iterBegin = nV.begin();
	iterEnd = nV.end();

	iter = std::search_n(iterBegin, iterEnd, 2, 14);	// 返回A处迭代器
	iter = std::search_n(iterBegin, iterEnd, 3, 14);	// 返回nV.end();

	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值