I want to check whether two vectors have any elements in common. What’s wrong with this syntax?
// Check whether the current list and the input l2 share any nodes or not
bool shared(const VectorList< NODETYPE > &l2);
template< typename NODETYPE > //SHARED
bool VectorList< NODETYPE>::shared(const VectorList< NODETYPE > &l2)
{
for(int i = 0; i < (int)vList.size(); i++)
{
for (int j = i; j < (int)l2.size() ; j++)
{
if (vList[i] == l2[j])
{
return(1);
}
}
}
return(0);
}
Assuming you have implemented VectorList as (similar to) a standard container, I’d consider writing (see
find_first_of):Note that the (worst-case) runtime complexity will still be quadratic (or
O(n*m))