fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <iterator>
  5. #include <fstream>
  6.  
  7. using namespace std;
  8.  
  9. void readSet(vector<int>& v, istream_iterator<int>& input) {
  10. int size = *input++;
  11. while (size--) {
  12. v.push_back(*input++);
  13. }
  14. }
  15.  
  16. int main() {
  17. ifstream file("plik.txt");
  18. if (!file.is_open()) {
  19. return 1;
  20. }
  21.  
  22. istream_iterator<int> input(file);
  23. vector<int> a, b;
  24. readSet(a, input);
  25. readSet(b, input);
  26.  
  27. sort(a.begin(), a.end());
  28. sort(b.begin(), b.end());
  29.  
  30. vector<int> intersection;
  31. set_intersection(
  32. a.begin(), a.end(),
  33. b.begin(), b.end(),
  34. back_inserter(intersection)
  35. );
  36.  
  37. cout << "Ilosc powtarzajacych sie: " << intersection.size() << "\n";
  38. cout << "Liczby:\n";
  39. copy(
  40. intersection.begin(),
  41. intersection.end(),
  42. ostream_iterator<int>(cout, "\n")
  43. );
  44. return 0;
  45. }
Runtime error #stdin #stdout 0s 3028KB
stdin
4

11 22 33 44

3

11 222 22
stdout
Standard output is empty