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

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

6. find 和find_if元素查找


使用元素的等于操作符, 对范围[first, last)内的元素与输入值val进行比较. 当匹配时,结束搜索, 返回该元素的一个InputIterator

函数原形
template<class InIt, class T> InIt find(InIt first, InIt last, const T& val);

// 使用输入的函数代替等于操作符执行find
template<class InIt, class Pred> InIt find_if(InIt first, InIt last, 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;

	int nVal = 6;

	// 升序
	nV.clear();
	nV.push_back(16);
	nV.push_back(16);
	nV.push_back(18);
	nV.push_back(18);
	nV.push_back(18);
	nV.push_back(18);
	nV.push_back(18);
	nV.push_back(19);

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

	iter = std::find(iterBegin, iterEnd, 10); 	// 返回 end
	iter = std::find(iterBegin, iterEnd, 16); 	// 返回 第一个16的InputIterator
	*iter = 20;							// 这里可以被修改

	return 0;
}

注意:
1. 返回第一个InputIterator
2. 所以返回的InputIterator中的值是可以修改的.

7. find_end(找到最后一组, 并返回)

在范围[first1, last1)内查找"[first2, last2)"的最后一次出现.找到则返回最后一对的第一个ForwardIterator,否则返回last1.

(这里使用等于操作符), 重载版本使用用户输入的操作符代替等于操作
函数原形
template<class FwdIt1, class FwdIt2> FwdIt1 find_end(FwdIt1 first1, FwdIt1 last1,FwdIt2 first2, FwdIt2 last2);
template<class FwdIt1, class FwdIt2, class Pred> FwdIt1 find_end(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::find_end(iterBegin, iterEnd, nV1.begin(), nV1.end());	// 返回上面"第2组"第一个元素所在的迭代器

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

	return 0;
}

8. find_first_of(找到其中一个, 并返回)

在范围[first1, last1)内查找"[first2, last2)"中任意一个元素的第一次出现, 如果存在则返回. 重载版本中使用了用户自定义操作符

函数原形
template<class FwdIt1, class FwdIt2> FwdIt1 find_first_of(FwdIt1 first1, FwdIt1 last1,FwdIt2 first2, FwdIt2 last2);
template<class FwdIt1, class FwdIt2, class Pred> FwdIt1 find_first_of(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);
	nV.push_back(14);
	nV.push_back(14);
	nV.push_back(16);
	nV.push_back(17);
	nV.push_back(16);
	nV.push_back(15);
	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(1);
	nV1.push_back(11);
	nV1.push_back(3);

	iter = std::find_first_of(iterBegin, iterEnd, nV1.begin(), nV1.end());	// 返回nV中的第一个元素的迭代器

	nV1.clear();
	nV1.push_back(40);
	nV1.push_back(1);
	nV1.push_back(3);
	nV1.push_back(2);
	iter = std::find_first_of(iterBegin, iterEnd, nV1.begin(), nV1.end());	// 返回end

	return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值