Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using std::vector;
- using std::pair;
- template <class T>
- class Set{
- private:
- vector<T> content;
- public:
- Set(vector<T> b){
- content = b;
- kill_repeats();
- }
- vector<T> to_vector(){
- std::sort(content.begin(), content.end());
- return content;
- }
- void add(T val){
- content.resize(content.size()+1);
- content[content.size()-1] = val;
- }
- void kill_repeats(){
- vector <T> tmp = to_vector();
- int ind = 0;
- for (int i=1; i<tmp.size(); i++){
- if (tmp[i-1] != tmp[i]) content[ind ++] = tmp[i-1];
- }
- content[ind ++] = tmp.back();
- content.resize(ind);
- }
- int get_size(){
- return (int)content.size();
- }
- };
- template <class T, class P>
- Set<pair<T,P>> dekart(Set<T> a, Set<P> b){
- vector<pair<T,P>> res(a.get_size() * b.get_size());
- int ind = 0;
- for (T x: a.to_vector()){
- for (P y: b.to_vector()){
- res[ind ++] = std::make_pair(x, y);
- }
- }
- return Set<pair<T,P>>(res);
- }
- using std::cin;
- using std::cout;
- int main(char* p){
- vector < int > v1 = {1, 3, 2, 5, 4, 5};
- vector < char > v2 = {'a', 'b', 'f', 'c', 'f', 'e'};
- Set<int> s1(v1);
- Set<char> s2(v2);
- cout << "First set:\n{ ";
- for (int i: s1.to_vector()){
- printf("'%d' ", i);
- }
- cout << "}\nSecond set:\n{ ";
- for (char i: s2.to_vector()){
- printf("'%c' ", i);
- }
- cout << "}\nDekart's product:\n";
- auto Dek = dekart(s1, s2);
- for (auto i: Dek.to_vector()){
- printf("('%d', '%c')\n", i.first, i.second);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment