本文内容来自C++Plus,本文只是本人的总结和翻译而已。本人只是C++的搬运工。
原文传送门:http://www.cplusplus.com/reference/algorithm/search_n
search_n算法:查找范围内的元素。
template<class ForwardIterator, class Size, class T>
ForwardIterator search_n(ForwardIterator first, ForwardIterator last,
Size count, const T& val)
{
ForwardIterator it, limit;
Size i;
limit = first; std::advance(limit, std::distance(first, last) - count);
while (first != limit)
{
it = first; i = 0;
while (*it == val) // or: while (pred(*it,val)) for the pred version
{
++it; if (++i == count) return first;
}
++first;
}
return last;