Advertisement
momo2345

STL

Aug 13th, 2020
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.56 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void insert(set<int> &s,int x);
  5.  
  6. void print_contents(set<int> &s);
  7.  
  8. void erase(set<int> &s,int x);
  9.  
  10. int find(set<int> &s,int x);
  11.  
  12. int size(set<int> &s);
  13.  
  14. int main() {
  15.     // your code goes here
  16.     int t;
  17.     cin>>t;
  18.     while(t--)
  19.     {
  20.         set<int> s;
  21.         int q;
  22.         cin>>q;
  23.         while(q--)
  24.         {
  25.             char c;
  26.             cin>>c;
  27.             if(c=='a')
  28.             {
  29.                 int x;
  30.                 cin>>x;
  31.                 insert(s,x);
  32.             }
  33.             if(c=='b')
  34.             {
  35.                 print_contents(s);
  36.             }
  37.             if(c=='c')
  38.             {
  39.                 int x;
  40.                 cin>>x;
  41.                 erase(s,x);
  42.             }
  43.             if(c=='d')
  44.             {
  45.                 int x;
  46.                 cin>>x;
  47.                 cout<<find(s,x)<<" ";
  48.             }
  49.                        if(c=='e')
  50.                        cout<<size(s)<<" ";
  51.        
  52.         }
  53. cout<<endl;
  54.     }
  55.     return 0;
  56. }// } Driver Code Ends
  57.  
  58.  
  59. /* You are required to complete below methods */
  60.  
  61. /*inserts an element x to the set s */
  62. void insert(set<int> &s,int x)
  63. {
  64.     //Your code here
  65.     s.insert(x);
  66. }
  67.  
  68. /*prints the contents of the set s */
  69. void print_contents(set<int> &s)
  70. {
  71.     //Your code here
  72.    for(auto u:s) cout<<u<<" ";
  73.  // for(auto i=s.begin();i!=s.end();i++)
  74.   //  cout<<*i<<" ";
  75. }
  76.  
  77. /*erases an element x from the set s */
  78. void erase(set<int> &s,int x)
  79. {
  80.     //Your code here
  81.     s.erase(x);
  82. }
  83.  
  84. /*returns 1 if the element x is
  85. present in set s else returns -1 */
  86. int find(set<int> &s,int x)
  87. {
  88.     //Your code here
  89.    // if(s.find()!=s.end()) return 1;
  90.    if(s.count(x)==1) return 1;
  91.    else  return -1;
  92. }
  93.  
  94. /*returns the size of the set s */
  95. int size(set<int> &s)
  96. {
  97.    //Your code here
  98.    return s.size();
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement