c++ - set_difference and set_intersection simultaneously -
i'm wondering if there facility in standard library simultaneously compute set intersection , set difference between 2 sorted ranges. signature along lines of:
template <class input1, class input2, class output1, class output2, class output3> output3 decompose_sets (input1 first1, input1 last1, input2 first2, input2 last2, output1 result1, output2 result2, output3 result3 );
such after call decompose sets
, result1
contains elements in [first1,last1)
not in [first2,last2)
, result2
contains elements in [first2,last2)
not in [first1,last1)
, , result3
contains element common in [first1,last1)
, [first2,last2)
.
the example implementations of set_difference
, set_intersection
cplusplus.com seem can me create efficient implementation performs 1 scan instead of three. if it's in standard library, though, i'd hate reinvent wheel.
example, request:
given 2 sets a={0, 1, 2, 3, 4} , b={2, 4, 5, 6} build following 3 sets:
- only_a = {0,1,3}
- only_b = {5,6}
- common = {2,4}
there's no standard library algorithm in single scan it's easy write. following looks correct , output makes sense here on ideone.com.
template <class input1, class input2, class output1, class output2, class output3> output3 decompose_sets(input1 first1, input1 last1, input2 first2, input2 last2, output1 result1, output2 result2, output3 result3) { while (first1 != last1 && first2 != last2) { if (*first1 < *first2) { *result1++ = *first1++; } else if (*first2 < *first1) { *result2++ = *first2++; } else { *result3++ = *first1++; ++first2; // skip common value in set2 } } std::copy(first1, last1, result1); std::copy(first2, last2, result2); return result3; }
Comments
Post a Comment