fork download
  1. template <typename InIter1, typename InIter2, typename OutIter>
  2. OutIter set_difference(InIter1 first1, InIter1 last1, InIter2 first2, InIter2 last2, OutIter out) {
  3. while (first1 != last1 and first2 != last2) {
  4. if (*first1 < *first2) {
  5. *out = *first1;
  6. ++first1;
  7. ++out;
  8. } else if (*first1 > *first2) {
  9. *out = *first2;
  10. ++first2;
  11. ++out;
  12. } else {
  13. ++first1;
  14. ++first2;
  15. }
  16. }
  17. //
  18. while (first1 == last1 and first2 != last2) {
  19. *out = *first2;
  20. ++first2;
  21. ++out;
  22. }
  23. //
  24. while (first1 != last1 and first2 == last2) {
  25. *out = *first1;
  26. ++first1;
  27. ++out;
  28. }
  29. return out;
  30. }
  31.  
  32. #include <vector>
  33. #include <iostream>
  34.  
  35. int main () {
  36. int first[] = {1, 2, 3};
  37. int second[] = {2, 4, 6, 8};
  38. std::vector<int> v(7); // 0 0 0 0 0 0 0 0 0 0
  39. std::vector<int>::iterator it;
  40.  
  41. //std::sort (first,first+5); // 5 10 15 20 25
  42. //std::sort (second,second+5); // 10 20 30 40 50
  43.  
  44. it=std::set_difference (first, first+3, second, second+4, v.begin());
  45. // 5 15 25 0 0 0 0 0 0 0
  46. v.resize(it-v.begin()); // 5 15 25
  47.  
  48. std::cout << "The difference has " << (v.size()) << " elements:\n";
  49. for (it=v.begin(); it!=v.end(); ++it)
  50. std::cout << ' ' << *it;
  51. std::cout << '\n';
  52.  
  53. return 0;
  54. }
  55.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:44:8: error: ‘set_difference’ is not a member of ‘std’
     it=std::set_difference (first, first+3, second, second+4, v.begin());
        ^~~
prog.cpp:44:8: note: suggested alternative:
prog.cpp:2:9: note:   ‘set_difference’
 OutIter set_difference(InIter1 first1, InIter1 last1, InIter2 first2, InIter2 last2, OutIter out) {
         ^~~~~~~~~~~~~~
stdout
Standard output is empty