Advertisement
kokokozhina

613

Jan 2nd, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <set>
  4.  
  5. using namespace std;
  6.  
  7. set<int> convert(string s)
  8. {
  9.     set<int> a;
  10.     s.erase(0, 1);
  11.     while(s.length())
  12.     {
  13.         int punc = min(s.find(','), s.find(']'));
  14.         string littles = s.substr(0, punc);
  15.         a.insert(stoi(littles));
  16.         s.erase(s.begin(), s.begin() + punc + 1);
  17.     }
  18.     return a;
  19. }
  20.  
  21. void print(set<int> a)
  22. {
  23.     cout << "[";
  24.     int counter = 0;
  25.     for(set<int>::iterator i = a.begin(); i != a.end(); i++)
  26.     {
  27.         counter++;
  28.         cout << *i;
  29.         if (counter < a.size())
  30.             cout << ",";
  31.     }
  32.     cout << "]\n";
  33. }
  34.  
  35.  
  36. int main()
  37. {
  38.     string A, B;
  39.     cin >> A >> B;
  40.     set<int> a, b, intersection, association, difference;
  41.     a = convert(A);
  42.     b = convert(B);
  43.  
  44.     for(set<int>::iterator i = a.begin(); i != a.end(); i++)
  45.     {
  46.         if (b.count(*i))
  47.             intersection.insert(*i);
  48.         else
  49.             difference.insert(*i);
  50.         association.insert(*i);
  51.     }
  52.     for(set<int>::iterator i = b.begin(); i != b.end(); i++)
  53.     {
  54.         association.insert(*i);
  55.     }
  56.  
  57.     print(intersection);
  58.     print(association);
  59.     print(difference);
  60.  
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement