描述:
在容器中寻找子序列首次出现的位置。
元素用 operator== 比较。
定义:
template< class ForwardIt1, class ForwardIt2 >
ForwardIt1 search( ForwardIt1 first, ForwardIt1 last,
ForwardIt2 s_first, ForwardIt2 s_last );
template< class ForwardIt1, class ForwardIt2 >
constexpr ForwardIt1 search( ForwardIt1 first, ForwardIt1 last,
ForwardIt2 s_first, ForwardIt2 s_last );
template< class ForwardIt1, class ForwardIt2, class BinaryPredicate >
ForwardIt1 search( ForwardIt1 first, ForwardIt1 last,
ForwardIt2 s_first, ForwardIt2 s_last, BinaryPredicate p );
template< class ForwardIt1, class ForwardIt2, class BinaryPredicate >
constexpr ForwardIt1 search( ForwardIt1 first, ForwardIt1 last,
ForwardIt2 s_first, ForwardIt2 s_last, BinaryPredicate p );
可能的实现:
template<class ForwardIt1, class ForwardIt2>
ForwardIt1 search(ForwardIt1 first, ForwardIt1 last,
ForwardIt2 s_first, ForwardIt2 s_last)
{
for (; ; ++first) {
ForwardIt1 it = first;
for (ForwardIt2 s_it = s_first; ; ++it, ++s_it) {
if (s_it == s_last) {
return first;
}
if (it == last) {
return last;
}
if (!(*it == *s_it)) {
break;
}
}
}
}
template<class ForwardIt1, class ForwardIt2, class BinaryPredicate>
ForwardIt1 search(ForwardIt1 first, ForwardIt1 last,
ForwardIt2 s_first, ForwardIt2 s_last,
BinaryPredicate p)
{
for (; ; ++first) {
ForwardIt1 it = first;
for (ForwardIt2 s_it = s_first; ; ++it, ++s_it) {
if (s_it == s_last) {
return first;
}
if (it == last) {
return last;
}
if (!p(*it, *s_it)) {
break;
}
}
}
}
参数:
first, last - 要检验的元素范围
s_first, s_last - 要搜索的元素范围
p - 若元素应被当做相等则返回 true 的二元谓词。
返回值:
返回范围中 [first, last - (s_last - s_first)) ,首个子序列 [s_first, s_last) 起始的迭代器。若找不到这种子序列,则返回 last 。
示例:
#include <iostream>
#include <algorithm>
#include <string>
int main()
{
std::string s = "abcdefghijklmn";
std::string s2 = "def";
std::string s3 = "deg";
auto iter1 = std::search(std::begin(s),std::end(s),std::begin(s2),std::end(s2));
if (iter1 == std::end(s))
std::cout << "s中不存在s2子序列" << std::endl;
else
{
int nIndex = std::distance(std::begin(s), iter1);
std::cout << "s2子序列在s中的索引位置:" << nIndex << std::endl;
}
//s2子序列在s中的索引位置:3
auto iter2 = std::search(std::begin(s), std::end(s), std::begin(s3), std::end(s3));
if (iter2 == std::end(s))
std::cout << "s中不存在s3子序列" << std::endl;
else
{
int nIndex = std::distance(std::begin(s), iter2);
std::cout << "s3子序列在s中的索引位置:" << nIndex << std::endl;
}
//s中不存在s3子序列
return 0;
}
#include <iostream>
#include <algorithm>
#include <vector>
struct SData
{
int m_nData;
double m_dData;
SData(int nData, double dData)
:m_nData(nData),m_dData(dData)
{
}
};
int main()
{
std::vector<SData> v, v2;
for (int i = 0; i < 5; i++)
{
SData data(i,i);
v.push_back(data);
}
SData data(2,2.0);
v2.push_back(data);
//使用lambda表达式指定条件(一般用于自定义元素)
auto iter = std::search(std::begin(v), std::end(v), std::begin(v2), std::end(v2), [](const SData& data1,const SData& data2)
{
return data1.m_nData == data2.m_nData;
});
if (iter == std::end(v))
std::cout << "v中不存在v2子序列" << std::endl;
else
{
int nIndex = std::distance(std::begin(v), iter);
std::cout << "v2子序列在v中的索引位置:" << nIndex << std::endl;
}
//v2子序列在v中的索引位置:2
return 0;
}