fork download
  1.  
  2. #include <iostream>
  3. #include <iterator>
  4. #include <set>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. // empty set container
  11. set<int, greater<int> > s1;
  12.  
  13. // insert elements in random order
  14. s1.insert(40);
  15. s1.insert(30);
  16. s1.insert(60);
  17. s1.insert(20);
  18. s1.insert(50);
  19.  
  20. // only one 50 will be added to the set
  21. s1.insert(50);
  22. s1.insert(10);
  23.  
  24. // printing set s1
  25. set<int, greater<int> >::iterator itr;
  26. cout << "\nThe set s1 is : \n";
  27. for (itr = s1.begin(); itr != s1.end(); itr++) {
  28. cout << *itr << " ";
  29. }
  30. cout << endl;
  31.  
  32. // assigning the elements from s1 to s2
  33. set<int> s2(s1.begin(), s1.end());
  34.  
  35. // print all elements of the set s2
  36. cout << "\nThe set s2 after assign from s1 is : \n";
  37. for (itr = s2.begin(); itr != s2.end(); itr++) {
  38. cout << *itr << " ";
  39. }
  40. cout << endl;
  41.  
  42. // remove all elements up to 30 in s2
  43. cout << "\ns2 after removal of elements less than 30 "
  44. ":\n";
  45. s2.erase(s2.begin(), s2.find(30));
  46. for (itr = s2.begin(); itr != s2.end(); itr++) {
  47. cout << *itr << " ";
  48. }
  49.  
  50. // remove element with value 50 in s2
  51. int num;
  52. num = s2.erase(50);
  53. cout << "\ns2.erase(50) : ";
  54. cout << num << " removed\n";
  55. for (itr = s2.begin(); itr != s2.end(); itr++) {
  56. cout << *itr << " ";
  57. }
  58.  
  59. cout << endl;
  60.  
  61. // lower bound and upper bound for set s1
  62. cout << "s1.lower_bound(40) : \n"
  63. << *s1.lower_bound(40) << endl;
  64. cout << "s1.upper_bound(40) : \n"
  65. << *s1.upper_bound(40) << endl;
  66.  
  67. // lower bound and upper bound for set s2
  68. cout << "s2.lower_bound(40) :\n"
  69. << *s2.lower_bound(40) << endl;
  70. cout << "s2.upper_bound(40) : \n"
  71. << *s2.upper_bound(40) << endl;
  72.  
  73. return 0;
  74. }
Success #stdin #stdout 0.01s 5504KB
stdin
Standard input is empty
stdout
The set s1 is : 
60 50 40 30 20 10 

The set s2 after assign from s1 is : 
10 20 30 40 50 60 

s2 after removal of elements less than 30 :
30 40 50 60 
s2.erase(50) : 1 removed
30 40 60 
s1.lower_bound(40) : 
40
s1.upper_bound(40) : 
30
s2.lower_bound(40) :
40
s2.upper_bound(40) : 
60