Advertisement
Hydron

Discrete_Math

Mar 25th, 2022
729
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using std::vector;
  4. using std::pair;
  5.  
  6. template <class T>
  7. class Set{
  8. private:
  9.     vector<T> content;
  10. public:
  11.     Set(vector<T> b){
  12.         content = b;
  13.         kill_repeats();
  14.     }
  15.  
  16.     vector<T> to_vector(){
  17.         std::sort(content.begin(), content.end());
  18.         return content;
  19.     }
  20.     void add(T val){
  21.         content.resize(content.size()+1);
  22.         content[content.size()-1] = val;
  23.     }
  24.     void kill_repeats(){
  25.         vector <T> tmp = to_vector();
  26.         int ind = 0;
  27.         for (int i=1; i<tmp.size(); i++){
  28.             if (tmp[i-1] != tmp[i]) content[ind ++] = tmp[i-1];
  29.         }
  30.         content[ind ++] = tmp.back();
  31.         content.resize(ind);
  32.     }
  33.     int get_size(){
  34.         return (int)content.size();
  35.     }
  36. };
  37.  
  38. template <class T, class P>
  39. Set<pair<T,P>> dekart(Set<T> a, Set<P> b){
  40.     vector<pair<T,P>> res(a.get_size() * b.get_size());
  41.     int ind = 0;
  42.     for (T x: a.to_vector()){
  43.         for (P y: b.to_vector()){
  44.             res[ind ++] = std::make_pair(x, y);
  45.         }
  46.     }
  47.     return Set<pair<T,P>>(res);
  48. }
  49.  
  50. using std::cin;
  51. using std::cout;
  52.  
  53. int main(char* p){
  54.     vector < int > v1 = {1, 3, 2, 5, 4, 5};
  55.     vector < char > v2 = {'a', 'b', 'f', 'c', 'f', 'e'};
  56.     Set<int> s1(v1);
  57.     Set<char> s2(v2);
  58.     cout << "First set:\n{ ";
  59.     for (int i: s1.to_vector()){
  60.         printf("'%d' ", i);
  61.     }
  62.     cout << "}\nSecond set:\n{ ";
  63.     for (char i: s2.to_vector()){
  64.         printf("'%c' ", i);
  65.     }
  66.     cout << "}\nDekart's product:\n";
  67.     auto Dek = dekart(s1, s2);
  68.     for (auto i: Dek.to_vector()){
  69.         printf("('%d', '%c')\n", i.first, i.second);
  70.     }
  71.     return 0;
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement