Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <iostream>
#include <iterator>
#include <vector>
template<typename InputIterator1, typename InputIterator2, typename OutputIterator>
OutputIterator my_set_difference(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator out)
{
auto current1 = first1;
auto current2 = first2;
while (current1 != last1)
{
if (current2 == last2) return std::copy(current1, last1, out);
if (*current1 < *current2)
{
*out = *current1;
++out;
++current1;
}
else
{
if (!(*current2 < *current1))
{
++current1;
}
++current2;
}
}
return out;
}
int main()
{
std::vector<int> A = {1, 2, 3, 6, 7, 7, 10, 11, 12};
std::vector<int> B = {4, 6, 7, 8};
my_set_difference(begin(A), end(A), begin(B), end(B), std::ostream_iterator<int>(std::cout, " "));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.