It would be impractical to manually sort the underlying vector after each addition/removal for the list. It would only be reasonable to do this if it were somehow possible to force
vector::push_back()to automatically insert elements such that insertions preseve the sorting of the list.
What you're talking about here is an ordered insert. There are functions in <algorithm> that allow you do do this. Rather than using std::vector::push_back you would use std::vector::insert, and call std::lower_bound which does a binary search for the first element not less than than a given value.
auto insert_pos = std::lower_bound( L2.begin(), L2.end(), value );
if( insert_pos == L2.end() || *insert_pos != value )
{
L2.insert( insert_pos, value );
}
This makes every insertion O(logN) but if you are doing fewer than N insertions between your periodic checks, it ought to be an improvement.
The zipping operation might look something like this:
auto it1 = L1.begin();
auto it2 = L2.begin();
while( it1 != L1.end() && it2 != L2.end() )
{
if( *it1 < *it2 ) {
Handle_Missing( *it1++ );
} else if( *it2 < *it1 ) {
Handle_New( *it2++ );
} else {
it1++;
it2++;
}
}
while( it1 != L1.end() ) Handle_Missing( *it1++ );
while( it2 != L2.end() ) Handle_New( *it2++ );
Answer from paddy on Stack OverflowHow do I compare two lists in C# - Questions & Answers - Unity Discussions
c - Compare 2 linked lists using loop - Stack Overflow
How do I compare two lists using iterators? c++ - Stack Overflow
comparing of each string in two lists - C++ Forum
Is this the same as a list comparator or list diff tool?
Can I compare lists that have leading spaces or extra whitespace?
apple and apple will be treated as the same item. If items still appear identical but won't match, they may contain invisible characters - use the Invisible Character Detector to find hidden characters like non-breaking spaces or zero-width spaces.How do I compare two Excel columns for differences?
.txt file and drag it onto the input box. If your data has multiple columns and you need to match rows by a key column, try the Column Compare tool.It would be impractical to manually sort the underlying vector after each addition/removal for the list. It would only be reasonable to do this if it were somehow possible to force
vector::push_back()to automatically insert elements such that insertions preseve the sorting of the list.
What you're talking about here is an ordered insert. There are functions in <algorithm> that allow you do do this. Rather than using std::vector::push_back you would use std::vector::insert, and call std::lower_bound which does a binary search for the first element not less than than a given value.
auto insert_pos = std::lower_bound( L2.begin(), L2.end(), value );
if( insert_pos == L2.end() || *insert_pos != value )
{
L2.insert( insert_pos, value );
}
This makes every insertion O(logN) but if you are doing fewer than N insertions between your periodic checks, it ought to be an improvement.
The zipping operation might look something like this:
auto it1 = L1.begin();
auto it2 = L2.begin();
while( it1 != L1.end() && it2 != L2.end() )
{
if( *it1 < *it2 ) {
Handle_Missing( *it1++ );
} else if( *it2 < *it1 ) {
Handle_New( *it2++ );
} else {
it1++;
it2++;
}
}
while( it1 != L1.end() ) Handle_Missing( *it1++ );
while( it2 != L2.end() ) Handle_New( *it2++ );
Can you create a hash value for your list items? If so, just compute the hash and check the hash table for the other list. This is quick, does not require sorting, and prevents your "every possible combination" problem. If your're using C++ and the STL you could use a map container to hold each list.
- Create a hash for each item in L1, and use
mapto map it associate it with your list item. - Create a similar map for L2, and as each L2 has is created check to see if it's in the L1 map.
- When a new element is added to L2, calculate its hash value and check to see if it's in the L1 hash map (using
map.find()if using STL maps). If not then carry out yourHandle_New_Element()function. - When an element is subtracted from the L2 list and it's hash is not in the L1 hash map then carry out your
Handle_Missing_Element()function.