fork(1) download
  1. #include <iostream>
  2. #include <set>
  3.  
  4. using namespace std;
  5.  
  6. void print(set<int>& s)
  7. {
  8. cout << "\ns = {";
  9. auto p = s.begin();
  10. while (p != s.end())
  11. cout << *p++ << ",}"[p==s.end()];
  12. }
  13.  
  14. void print(multiset<int>& s)
  15. {
  16. cout << "\nms = {";
  17. auto p = s.begin();
  18. while (p != s.end())
  19. cout << *p++ << ",}"[p==s.end()];
  20. }
  21.  
  22. int main()
  23. {
  24.  
  25. int a[] = { 7, 4, 9, 1, 1, 4, 8 };
  26. set<int> s(a, a + 7);
  27. print(s);
  28. s.insert(3);
  29. print(s);
  30. s.erase(3);
  31. print(s);
  32. set<int>::iterator ix = s.find(4);
  33. s.erase(ix);
  34. print(s);
  35. s.erase(s.find(7), s.find(9));
  36. print(s);
  37. cout << "\nCount of 1: " << s.count(1);
  38. cout << "\nCount of 2: " << s.count(2);
  39. s.insert(2);
  40. s.insert(4);
  41. s.insert(5);
  42. s.insert(7);
  43. print(s);
  44.  
  45. auto it = s.lower_bound(5);
  46. cout << "\nThe lower bound of 5 is " << *it << ".";
  47. it = s.lower_bound(6);
  48. cout << "\nThe lower bound of 6 is " << *it << ".";
  49. it = s.lower_bound(10);
  50. if (it == s.end()) cout << "\nThe lower bound of 10 is at the end of the range.";
  51.  
  52. it = s.upper_bound(5);
  53. cout << "\nThe upper bound of 5 is " << *it << ".";
  54. it = s.upper_bound(6);
  55. cout << "\nThe upper bound of 6 is " << *it << ".";
  56. it = s.upper_bound(10);
  57. if (it == s.end()) cout << "\nThe upper bound of 10 is at the end of the range.";
  58.  
  59. auto it_pair = s.equal_range(5);
  60. cout << "\nThe bounds of 5 are " << *it_pair.first << " and " << *it_pair.second << ".";
  61.  
  62. int b[] = {1, 1, 2, 3, 4, 4, 4, 5};
  63. multiset<int> ms(b, b+8);
  64. print(ms);
  65.  
  66. auto p = ms.lower_bound(4);
  67. cout<<"\n";
  68. while (p != ms.upper_bound(4))
  69. cout << *p++ << " ";
  70. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
s = {1,4,7,8,9}
s = {1,3,4,7,8,9}
s = {1,4,7,8,9}
s = {1,7,8,9}
s = {1,9}
Count of 1: 1
Count of 2: 0
s = {1,2,4,5,7,9}
The lower bound of 5 is 5.
The lower bound of 6 is 7.
The lower bound of 10 is at the end of the range.
The upper bound of 5 is 7.
The upper bound of 6 is 7.
The upper bound of 10 is at the end of the range.
The bounds of 5 are 5 and 7.
ms = {1,1,2,3,4,4,4,5}
4 4 4