我刚刚从http://www.cplusplus.com/reference/algorithm/search/复制了'搜索'查找子序列的算法。
看起来像是:
template<class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 search ( ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2)
{
if (first2==last2) return first1; // specified in C++11
while (first1!=last1)
{
ForwardIterator1 it1 = first1;
ForwardIterator2 it2 = first2;
while (*it1==*it2) { // or: while (pred(*it1,*it2)) for version 2
if (it2==last2) return first1;
if (it1==last1) return last1;
++it1; ++it2;
}
++first1;
}
return last1;
}
我已尝试将此算法与visual studio一起使用,但在调试结束时,它会因以下调试错误而崩溃:
vector iterator not dereferencable
我发起一些向量的代码是:
vector<int> v1 = { 1,4,7,10,5,0};
vector<int>v2 = {10,5,0};
vector<int>::iterator it = cpp_search(v1.begin(), v1.end(), v2.begin(), v2.end());
可能是什么问题?