函数原型
//无谓词
//查找 [first1, last1) 范围内第一个 [first2, last2) 子序列
template<class _FwdItHaystack,class _FwdItPat>
_NODISCARD inline _FwdItHaystack search(const _FwdItHaystack _First1, const _FwdItHaystack _Last1, const _FwdItPat _First2, const _FwdItPat _Last2)
//谓词
//查找 [first1, last1) 范围内,和 [first2, last2) 序列满足pred规则的第一个子序列
template<class _FwdItHaystack, class _FwdItPat,class _Pr>
_NODISCARD inline _FwdItHaystack search(_FwdItHaystack _First1, const _FwdItHaystack _Last1, const _FwdItPat _First2, const _FwdItPat _Last2, _Pr _Pred)
template<class _FwdItHaystack, class _Searcher>
_NODISCARD inline _FwdItHaystack search(const _FwdItHaystack _First, const _FwdItHaystack _Last, const _Searcher& _Search)
如下两个序列为例:
序列 1:1,2,2,3,4,5,1,2,2,3,4,5
序列 2:1,2,2
用于在序列 1 中查找序列 2 第一次出现的位置。
可以看到,序列 2 在序列 1 中出现了 2 次。借助 find_end() 函数,我们可以找到序列 1 中最后一个(也就是第 2 个){1,2,2};而借助 search() 函数,我们可以找到序列 1 中第 1 个 {1,2,2}。
参数
first1、last1:都为正向迭代器,其组合 [first1, last1) 用于指定查找范围(即序列 1);
first2、last2:都为正向迭代器,其组合 [first2, last2) 用于指定要查找的序列(即序列 2);
pred:自定义查找规则。该规则实际上是一个包含 2 个参数且返回值类型为 bool 的函数(第一个参数接收 [first1, last1) 范围内的元素,第二个参数接收 [first2, last2) 范围内的元素)。函数定义的形式可以是普通函数,也可以是函数对象。
无谓词
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
int main()
{
std::vector<int> aa{ 1, 2, 2, 3, 4, 5, 1, 2, 2, 3, 4, 5 };
int arr[] = { 1, 2, 2 };
auto it = std::search(std::begin(aa), std::end(aa), std::begin(arr), std::end(arr));
if (it == std::end(aa))
{
std::cout << "No matching elements\n";
}
else
{
std::cout << "The first matching elements is at: "
<< std::distance(std::begin(aa), it) << ", *it = "
<< *it << '\n';
}
std::cout << std::endl;
return -1;
}
//输出
The first matching elements is at: 0, *it = 1
谓词
函数
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
bool twice(int a, int b)
{
std::cout << "a = " << a << "; b = " << b << std::endl;
return b == a * 2;
}
int main()
{
std::vector<int> aa{ 1, 2, 2, 3, 4, 5, 2, 4, 4, 3, 4, 5 };
int arr[] = { 4, 8, 8 };
auto it = std::search(std::begin(aa), std::end(aa), std::begin(arr), std::end(arr), twice);
if (it == std::end(aa))
{
std::cout << "No matching elements\n";
}
else
{
std::cout << "The first matching elements is at: "
<< std::distance(std::begin(aa), it) << ", *it = "
<< *it << '\n';
}
std::cout << std::endl;
std::function<bool(int, int)> f = twice;
auto it1 = std::search(std::begin(aa), std::end(aa), std::begin(arr), std::end(arr), f);
if (it1 == std::end(aa))
{
std::cout << "No matching elements\n";
}
else
{
std::cout << "The first matching elements is at: "
<< std::distance(std::begin(aa), it1) << ", *it = "
<< *it1 << '\n';
}
std::cout << std::endl;
return -1;
}
//输出
a = 1; b = 4
a = 2; b = 4
a = 2; b = 8
a = 2; b = 4
a = 3; b = 8
a = 3; b = 4
a = 4; b = 4
a = 5; b = 4
a = 2; b = 4
a = 4; b = 8
a = 4; b = 8
The first matching elements is at: 6, *it = 2
a = 1; b = 4
a = 2; b = 4
a = 2; b = 8
a = 2; b = 4
a = 3; b = 8
a = 3; b = 4
a = 4; b = 4
a = 5; b = 4
a = 2; b = 4
a = 4; b = 8
a = 4; b = 8
The first matching elements is at: 6, *it = 2
仿函数
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
class Twice
{
public:
bool operator()(int a, int b)
{
std::cout << "a = " << a << "; b = " << b << std::endl;
return b == a * 2;
}
};
int main()
{
std::vector<int> aa{ 1, 2, 2, 3, 4, 5, 2, 4, 4, 3, 4, 5 };
int arr[] = { 4, 8, 8 };
auto it = std::search(std::begin(aa), std::end(aa), std::begin(arr), std::end(arr), Twice());
if (it == std::end(aa))
{
std::cout << "No matching elements\n";
}
else
{
std::cout << "The first matching elements is at: "
<< std::distance(std::begin(aa), it) << ", *it = "
<< *it << '\n';
}
std::cout << std::endl;
return -1;
}
//输出
a = 1; b = 4
a = 2; b = 4
a = 2; b = 8
a = 2; b = 4
a = 3; b = 8
a = 3; b = 4
a = 4; b = 4
a = 5; b = 4
a = 2; b = 4
a = 4; b = 8
a = 4; b = 8
The first matching elements is at: 6, *it = 2
bind
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
bool twice(int a, int b)
{
std::cout << "a = " << a << "; b = " << b << std::endl;
return b == a * 2;
}
class Twice
{
public:
bool operator()(int a, int b)
{
std::cout << "a = " << a << "; b = " << b << std::endl;
return b == a * 2;
}
};
int main()
{
std::vector<int> aa{ 1, 2, 2, 3, 4, 5, 2, 4, 4, 3, 4, 5 };
int arr[] = { 4, 8, 8 };
auto f1 = std::bind(twice, std::placeholders::_1, std::placeholders::_2);
auto it = std::search(std::begin(aa), std::end(aa), std::begin(arr), std::end(arr), f1);
if (it == std::end(aa))
{
std::cout << "No matching elements\n";
}
else
{
std::cout << "The first matching elements is at: "
<< std::distance(std::begin(aa), it) << ", *it = "
<< *it << '\n';
}
std::cout << std::endl;
auto f2 = std::bind(Twice(), std::placeholders::_1, std::placeholders::_2);
auto it1 = std::search(std::begin(aa), std::end(aa), std::begin(arr), std::end(arr), f2);
if (it1 == std::end(aa))
{
std::cout << "No matching elements\n";
}
else
{
std::cout << "The first matching elements is at: "
<< std::distance(std::begin(aa), it1) << ", *it = "
<< *it1 << '\n';
}
return -1;
}
//输出
a = 1; b = 4
a = 2; b = 4
a = 2; b = 8
a = 2; b = 4
a = 3; b = 8
a = 3; b = 4
a = 4; b = 4
a = 5; b = 4
a = 2; b = 4
a = 4; b = 8
a = 4; b = 8
The first matching elements is at: 6, *it = 2
a = 1; b = 4
a = 2; b = 4
a = 2; b = 8
a = 2; b = 4
a = 3; b = 8
a = 3; b = 4
a = 4; b = 4
a = 5; b = 4
a = 2; b = 4
a = 4; b = 8
a = 4; b = 8
The first matching elements is at: 6, *it = 2
lambda
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
int main()
{
std::vector<int> aa{ 1, 2, 2, 3, 4, 5, 2, 4, 4, 3, 4, 5 };
int arr[] = { 4, 8, 8 };
auto it = std::search(std::begin(aa), std::end(aa), std::begin(arr), std::end(arr),
[](int a, int b) {
std::cout << "a = " << a << "; b = " << b << std::endl;
return b == a * 2; });
if (it == std::end(aa))
{
std::cout << "No matching elements\n";
}
else
{
std::cout << "The first matching elements is at: "
<< std::distance(std::begin(aa), it) << ", *it = "
<< *it << '\n';
}
std::cout << std::endl;
return -1;
}
//输出
a = 1; b = 4
a = 2; b = 4
a = 2; b = 8
a = 2; b = 4
a = 3; b = 8
a = 3; b = 4
a = 4; b = 4
a = 5; b = 4
a = 2; b = 4
a = 4; b = 8
a = 4; b = 8
The first matching elements is at: 6, *it = 2