Last active
June 5, 2023 16:21
-
-
Save joboccara/116bd82e7a55fc8547103f191d0b8256 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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