Advertisement
_Muhammad

Everything about multiset

Jun 12th, 2020
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. /// *** --- ||| In the name of ALLAH ||| --- *** ///
  2.  
  3.  
  4.  
  5. #include<bits/stdc++.h>
  6. using namespace std;
  7.  
  8.  
  9. typedef long long ll;
  10. typedef vector<int> vi;
  11. typedef vector<ll> vl;
  12. typedef vector<vi> vvi;
  13. typedef vector<vl> vvl;
  14. typedef pair<int,int> pii;
  15. typedef pair<double, double> pdd;
  16. typedef pair<ll, ll> pll;
  17. typedef vector<pii> vii;
  18. typedef vector<pll> vll;
  19. typedef double dl;
  20.  
  21. #define endl '\n'
  22. #define PB push_back
  23. #define F first
  24. #define S second
  25. #define all(a) (a).begin(),(a).end()
  26. #define rall(a) (a).rbegin(),(a).rend()
  27. #define sz(x) (int)x.size()
  28.  
  29. const double PI = acos(-1);
  30. const double eps = 1e-9;
  31. const int inf = 2000000000;
  32. const ll infLL = 9000000000000000000;
  33. #define MOD 1000000007
  34.  
  35. #define mem(a,b) memset(a, b, sizeof(a) )
  36. #define sqr(a) ((a) * (a))
  37.  
  38. #define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  39. #define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield);
  40. #define file() freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
  41.  
  42. #define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)
  43. void faltu () { cerr << endl;}
  44. template < typename T, typename ... hello>void faltu( T arg, const hello &... rest) {cerr << arg << ' ';faltu(rest...);}
  45.  
  46. ll gcd ( ll a, ll b ) { return __gcd ( a, b ); }
  47. ll lcm ( ll a, ll b ) { return a * ( b / gcd ( a, b ) ); }
  48.  
  49. int main()
  50. {
  51. optimize();
  52.  
  53. /// Declare multiset
  54. multiset<int> s;
  55.  
  56. /// insert in multiset.
  57. s.insert( 1 );
  58. s.insert( 4 );
  59. s.insert( 2 );
  60. s.insert( 2 );
  61.  
  62. /// Print multiset size and its elements using for each loop
  63. cout << s.size() << endl; /// 4
  64. for ( auto u : s ) cout << u << " "; /// 1 2 2 4
  65. cout << endl;
  66.  
  67. ///Print it elements using iterator
  68. set<int>::iterator it;
  69. for ( it = s.begin(); it != s.end(); it++ ) cout << *it << " "; /// 1 2 2 4
  70. cout << endl;
  71.  
  72. /// element erase in multiset
  73. s.erase( 2 );
  74. cout << s.size() << endl; /// 2
  75. for ( auto u : s ) cout << u << " "; /// 1 4
  76. cout << endl;
  77.  
  78. s.insert( 2 );
  79. s.insert( 2 );
  80.  
  81. it = s.find( 2 );
  82. if ( it != s.end() ) s.erase( it );
  83.  
  84. cout << s.size() << endl; /// 3
  85. for ( auto u : s ) cout << u << " "; /// 1 2 4
  86. cout << endl;
  87.  
  88. s.erase( 5 );
  89. cout << s.size() << endl; /// 3
  90. for ( auto u : s ) cout << u << " "; /// 1 2 4
  91. cout << endl;
  92.  
  93. /// another way of multiset declare and assigning a multiset into another
  94. multiset<int> tmp = { 6, 6, 1, 2, 3, 4, 5 };
  95. s = tmp;
  96. cout << s.size() << endl; /// 7
  97. for ( auto u : s ) cout << u << " "; /// 1 2 3 4 5 6 6
  98. cout << endl;
  99.  
  100. /// Finding pointer where element 2 is located
  101. it = s.find( 2 );
  102.  
  103. /// Checking is a element exist in multiset
  104. if ( it != s.end() ) cout << "Element exist\n";
  105. else cout << "Element not exist\n";
  106.  
  107. /// Counting number of occurrence's of an element of multiset
  108. cout << s.count( 6 ) << endl; /// 2
  109. cout << s.count( 10 ) << endl; /// 0
  110.  
  111. /// Checking is a multiset is empty of not
  112. cout << s.empty() << endl; /// 0
  113.  
  114. /// Clearing full multiset
  115. s.clear();
  116. cout << s.empty() << endl; /// 1
  117.  
  118. /// Printing smallest element in multiset
  119. s = tmp;
  120. cout << *s.begin() << endl; /// 1
  121.  
  122. /// Printing largest element in a multiset
  123. cout << *(--s.end() ) << endl; /// 6
  124. cout << *s.rbegin() << endl; /// 6
  125.  
  126. return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement