fork download
  1. #include <iostream>
  2. #include <iterator>
  3.  
  4. //пересечение упорядоченных массивов
  5. int* array_intersect(const int* f1, const int* l1,
  6. const int* f2, const int* l2, int* dst){
  7. while((f1 != l1) && (f2 != l2)){
  8. if(*f1 < *f2)
  9. ++f1;
  10. else {
  11. if(*f1 == *f2)
  12. *dst++ = *f1++;
  13. ++f2;
  14. }
  15. }
  16. return dst;
  17. }
  18.  
  19. //объединение упорядоченных массивов
  20. int* array_union(const int* f1, const int* l1,
  21. const int* f2, const int* l2, int* dst){
  22. while((f1 != l1) && (f2 != l2)){
  23. if(*f1 < *f2)
  24. *dst++ = *f1++;
  25. else {
  26. if(*f1 == *f2)
  27. ++f1;
  28. *dst++ = *f2++;
  29. }
  30. }
  31.  
  32. while(f1 != l1)
  33. *dst++ = *f1++;
  34. while(f2 != l2)
  35. *dst++ = *f2++;
  36. return dst;
  37. }
  38.  
  39. int main(void){
  40. int a[] = { 0, 1, 3, 5, 6, 7, 8, 9, 10, 50 };
  41. int b[] = { 0, 1, 2, 3, 4, 7, 8, 9, 50 };
  42. int c[sizeof(a)/sizeof(a[0]) + sizeof(b)/sizeof(b[0])];
  43.  
  44. int* e = array_intersect(a, a + sizeof(a)/sizeof(a[0]),
  45. b, b + sizeof(b)/sizeof(b[0]), c);
  46. std::copy(c, e, std::ostream_iterator<int>(std::cout, " "));
  47. std::cout << std::endl;
  48.  
  49. e = array_union(a, a + sizeof(a)/sizeof(a[0]),
  50. b, b + sizeof(b)/sizeof(b[0]), c);
  51. std::copy(c, e, std::ostream_iterator<int>(std::cout, " "));
  52. return 0;
  53. }
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
0 1 3 7 8 9 50 
0 1 2 3 4 5 6 7 8 9 10 50