I have two lists, L 1 and L 2data containing multiple elements, each unique, abstract data type (i.e. structs). Each of the two lists:
- May contain from zero to one hundred (inclusive) elements.
- It does not contain duplicate elements (each element is unique).
- May or may not contain items in another list (i.e.: L 1 and L 2 may be identical or contain completely different elements).
- Not sorted.
- The container is stored at the lowest level
std::vector<myStruct>.
I usually expect a new item to be added periodically to L 2, or the item is subtracted / removed from it. I am trying to identify the differences in the two lists as efficiently as possible (i.e. with minimal comparisons):
- If there is no entry in L 2 and is present in L 1, Perform one operation:
Handle_Missing_Element(). - If the entry is in L 2 and not present in L 1, Perform other operations:
Handle_New_Element().
After performing the above checks L 1 set to L 2, and after some time in the future L 2 checked again.
How can I make out the differences between the two lists? There are two approaches that I can think of:
- Compare both lists through each possible combination of elements. Perhaps O (n 2 ) is the execution complexity (terrible).
bool found;
for i in 1 .. L2->length()
found = false;
for j in 1 .. L1->length()
if (L1[j] == L2[i]
found = true;
fi
endfor
endfor
- -, . , . , , . / . ,
vector::push_back() , .
++? , , , , , , "" "" .
.