Advertisement
Imran_Mohammed

Everything_of_set

Jan 28th, 2021 (edited)
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.43 KB | None | 0 0
  1.       // In The Name Of Allah
  2.  
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5.  
  6.  
  7.  
  8. int main()
  9. {
  10.  
  11.  // Complexity __ ( log2 n )
  12.     //SET Is Always Unique And Sorted Element :
  13.  
  14.     //SET Declare :
  15.     set <int> s = {5,3,3,2,3};
  16.     cout << s.size() << endl;//3
  17.     for(auto u: s)cout << u << " ";//2 3 5
  18.     cout << endl;
  19.  
  20.  
  21.     //Find element : Complexity log2(n)
  22.     cout << s.count(2) << endl;//1
  23.     cout << s.count(4) << endl;// 0
  24.     //Find First Element :
  25.     cout << *s.begin() << endl;//2
  26.       //Find Last Element :
  27.       cout << *(--s.end()) << endl;//5
  28.       cout << *s.rbegin() << endl;//5
  29.  
  30.       //Erase Element :
  31.       s.erase(s.begin());
  32.       cout << s.size() << endl;//2
  33.       for(auto u: s)cout << u <<" ";//3 5
  34.       cout << endl;
  35.  
  36.      //Specific element delete :
  37.      int x;
  38.      s.erase(x);
  39.  
  40.  
  41.     //s.clear() = set clearing operation
  42.     //s.empty() = set empty or not
  43.  
  44.     // set user input :
  45.     set <int> se;
  46.     int n;
  47.     cin >> n;
  48.     for(int i=0; i<n; i++){
  49.         int a;
  50.         cin >> a;
  51.         se.insert(a);//Complexity log2(n)
  52.     }
  53.     for(auto u:se )cout << u << " ";
  54.     cout << endl;
  55.  
  56.     //string of set:
  57.     set <string> ss;
  58.     ss.insert("shahid");
  59.     ss.insert("imran");
  60.     ss.insert("shahid");
  61.     ss.insert("absar");
  62.     ss.insert("tawhid");
  63.     ss.insert("imran");
  64.     cout << ss.size() << endl;
  65.     for(auto u : ss)cout << u << " ";//absar imran shahid tawhid
  66.     cout << endl;
  67.  
  68.     //set of pair(pair of integer):
  69.     set<pair <int,int> >sp;
  70.     sp.insert({2,3});
  71.     sp.insert({4,3});
  72.     sp.insert({4,3});
  73.     sp.insert({2,7});
  74.     sp.insert({4,3});
  75.     sp.insert({5,1});
  76.     sp.insert({5,1});
  77.     cout << sp.size() << endl;//4
  78.     for(auto u:sp)cout << u.first <<" " << u.second << endl;/*
  79.     /* 2 3
  80.        2 7
  81.        4 3
  82.        5 1*/
  83.  
  84.     //Decreasing order in set(integer,pair,string etc):
  85.     set <int,greater<int> >sd;
  86.     sd.insert(2);
  87.     sd.insert(6);
  88.     sd.insert(2);
  89.     sd.insert(3);
  90.     sd.insert(5);
  91.     sd.insert(6);
  92.     cout << sd.size() << endl;//4
  93.     for(auto u : sd)cout << u << " ";//6 5 3 2
  94.     cout << endl;
  95.  
  96.     //Unordered set:(In this set elements are unique but not sort)
  97.     unordered_set <int>su;
  98.     su.insert(3);
  99.     su.insert(3);
  100.     su.insert(1);
  101.     su.insert(5);
  102.     su.insert(2);
  103.     cout << su.size() << endl;//4
  104.     for(auto u : su)cout << u << " ";//2 5 1 3
  105.     cout << endl;
  106.  
  107.     return 0;
  108. }
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement