fork(1) download
  1. #include <iostream>
  2. #include<ext/pb_ds/assoc_container.hpp>
  3. #include<ext/pb_ds/tree_policy.hpp>
  4. using namespace std;
  5.  
  6. using namespace __gnu_pbds;
  7. template<typename T> using ordered_multiset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  8. // Erase -
  9. // if(os.upper_bound(x)!=os.end())
  10. // os.erase(os.upper_bound(x));
  11.  
  12.  
  13. int main() {
  14. ordered_multiset<int>ms;
  15. vector<int> v{1, 1, 2, 2, 3, 4, 5, 6, 6};
  16.  
  17. for(int i : v) ms.insert(i);
  18.  
  19. cout << "Ordered Multiset: ";
  20. for(auto x : ms) cout << x << ' '; cout << '\n';
  21.  
  22. cout << "Lower bound of 2: " << *ms.lower_bound(2) << '\n';
  23. cout << "Upper bound of 2: " << *ms.upper_bound(2) << '\n';
  24.  
  25. if (ms.find(6) != ms.end()) {
  26. ms.erase(6);
  27. ms.insert(10);
  28. }
  29.  
  30. cout << "Ordered Multiset: ";
  31. for(auto x : ms) cout << x << ' '; cout << '\n';
  32.  
  33. return 0;
  34. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Ordered Multiset: 1 2 3 4 5 6 
Lower bound of 2: 2
Upper bound of 2: 3
Ordered Multiset: 1 2 3 4 5 10