fork(1) download
  1. #include <iostream>
  2. #include <set>
  3. #include <algorithm>
  4. #include <iterator>
  5. #include <fstream>
  6.  
  7. using namespace std;
  8.  
  9. void readSet(set<int>& v, istream_iterator<int>& input) {
  10. int size = *input++;
  11. while (size--) {
  12. v.insert(*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. set<int> a, b;
  24. readSet(a, input);
  25. readSet(b, input);
  26.  
  27. set<int> intersection;
  28. set_intersection(
  29. a.begin(), a.end(),
  30. b.begin(), b.end(),
  31. inserter(intersection, intersection.end())
  32. );
  33.  
  34. cout << "Ilosc powtarzajacych sie: " << intersection.size() << "\n";
  35. cout << "Liczby:\n";
  36. copy(
  37. intersection.begin(),
  38. intersection.end(),
  39. ostream_iterator<int>(cout, "\n")
  40. );
  41. return 0;
  42. }
Runtime error #stdin #stdout 0s 2984KB
stdin
4

11 22 33 44

3

11 222 22
stdout
Standard output is empty