I am new to C++. I am trying to find the index of the element if there is a subset in the vector.
I have my code below.. Please help me with the solution.
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<uint8_t> v = { 1,2,3,4,0,6,7,8,4,5,6 };
int key = 4;
std::vector<uint8_t>::iterator itr = std::find(v.begin(), v.end(), key);
if (itr != v.cend()) {
uint8_t index = std::distance(v.begin(), itr);
if ((++index == 5) && (++index == 6))
std::cout << "Element present at index " << index-2;
else
continue;
}
else {
std::cout << "Element not found";
}
return 0;
}
In the above code, I want to print the index of the element '4' if 4,5,6 are consecutive elements.
Output : 8