Skip to content

Instantly share code, notes, and snippets.

@joboccara
Last active June 5, 2023 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joboccara/116bd82e7a55fc8547103f191d0b8256 to your computer and use it in GitHub Desktop.
Save joboccara/116bd82e7a55fc8547103f191d0b8256 to your computer and use it in GitHub Desktop.
#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