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;
}