Suppose that I have two lists of objects and I would like to match every object in list one with every object in list two.
This would probably be the algorithm that one would immediately come up with.
for( it_1=list_1.begin() ; it_1!=list_1.end() ; it_1++ )
{
for( it_2=list_2.begin() ; it_2!=list_2.end() ; it_2++ )
{
//now match
match(*it_1,*it_2);
}
}
I wonder if there is any better way of doing this. This requires O(n1*n2), where n1 and n2 are the length of list_1 and list_2, respectively.
matchfunction do?matchcan be anything, then this algorithm (ignoring the typos in the outer loop increment) is asymptotically optimal. To improve it, you need to have knowledge of what the code inside the loop is doing.