This is the code you need to find all the elements:
for (auto it = v2.begin (); it != v2.end (); it++) {
auto fit = std::find (v1.begin (), v1.end (), *it); if (fit != v1.end ())
{
std::cout << "Element " << *fit << " found at position : ";
std::cout << it - v2.begin() << " (counting from zero) \n" ;
}
else
{
std::cout << "Element " << *it << " not found.\n";
}
}
Note that you cannot use
v2.erase
in the else clause as that will destroy the iterator. You need to make a copy of all the positions that need to be removed, and delete them later when this loop has completed.