描述:
在容器中寻找子序列最后出现的位置。
用 operator== 比较元素。
定义:
template< class ForwardIt1, class ForwardIt2 >
ForwardIt1 find_end( ForwardIt1 first, ForwardIt1 last,
ForwardIt2 s_first, ForwardIt2 s_last );
template< class ForwardIt1, class ForwardIt2 >
constexpr ForwardIt1 find_end( ForwardIt1 first, ForwardIt1 last,
ForwardIt2 s_first, ForwardIt2 s_last );
template< class ForwardIt1, class ForwardIt2, class BinaryPredicate >
ForwardIt1 find_end( ForwardIt1 first, ForwardIt1 last,
ForwardIt2 s_first, ForwardIt2 s_last, BinaryPredicate p );
template< class ForwardIt1, class ForwardIt2, class BinaryPredicate >
constexpr ForwardIt1 find_end( ForwardIt1 first, ForwardIt1 last,
ForwardIt2 s_first, ForwardIt2 s_last, BinaryPredicate p );
可能的实现:
template<class ForwardIt1, class ForwardIt2>
ForwardIt1 find_end(ForwardIt1 first, ForwardIt1 last,
ForwardIt2 s_first, ForwardIt2 s_last)
{
if (s_first == s_last)
return last;
ForwardIt1 result = last;
while (true) {
ForwardIt1 new_result = std::search(first, last, s_first, s_last);
if (new_result == last) {
break;
} else {
result = new_result;
first = result;
++first;
}
}
return result;
}
template<class ForwardIt1, class ForwardIt2, class BinaryPredicate>
ForwardIt1 find_end(ForwardIt1 first, ForwardIt1 last,
ForwardIt2 s_first, ForwardIt2 s_last,
BinaryPredicate p)
{
if (s_first == s_last)
return last;
ForwardIt1 result = last;
while (true) {
ForwardIt1 new_result = std::search(first, last, s_first, s_last, p);
if (new_result == last) {
break;
} else {
result = new_result;
first = result;
++first;
}
}
return result;
}
参数:
first, last - 要检验的元素范围
s_first, s_last - 要搜索的元素范围
p - 若元素应被当做相等则返回 true 的二元谓词。
返回值:
返回范围 [first, last) 中 [s_first, s_last) 最后一次出现的开端的迭代器。若找不到这种序列,则返回 last。
示例:
#include <iostream>
#include <algorithm>
#include <string>
int main()
{
std::string s = "abcdefgabcdefg";
std::string s2 = "def";
std::string s3 = "deg";
auto iter1 = std::find_end(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中的索引位置:10
auto iter2 = std::find_end(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);
}
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::find_end(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中的索引位置:7
return 0;
}