So I'm doing this assignment for a class, and need to write a function that will basically find all elements of a vector in another vector in the same exact order, then return the starting position of the sequence. Here's what I've tried:
It works fine as long as there are no repeated elements. For example, if I'm looking for a vector{1, 2, 3, 4} within vector {5, 6, 7, 1, 2, 3, 4, 2, 3}, it will come out fine, but if I try it with vector{1, 2, 3, 1, 2, 3, 4}, it fails on me.
Code:
for (int k = 0; k != v1.size(); k++)
{
if (v2[0] == v1[k])
{
int l = k;
int j = 0;
while (v1[l] == v2[j])
{
while (l != v1.size() && j != v2.size())
{
l++;
j++;
}
return k;
}
}
}
Comment